Python Package - Import

Card Puncher Data Processing

About

import functionality in Python

In order to use the names (generally, variable and function definitions) defined in a module, you must either:

  • import the module itself
  • or import the specific definitions,

you want to use from the module.

Configuration

By default, no import is allowed. You need to add a Python Package - init .py to the directory package

Syntax

No import

if you don't import the module, you must refer to a procedure or variable defined therein by using its qualified name.

The module

import myModuleName
myModuleName.func()

A name (function, variable,…)

  • Importing only a function of the module. Import statement that imports names from a module directly into the importing module’s symbol table.
from module import func1, func2

# Example
from math import sqrt
print sqrt(25)  # The full qualified name is here not needed

From source

import imp
imp.load_source('util', 'C:/full/path/foo/util.py')
import util

Relative

from . import mymodule
  • Class
from .file import myClass

Absolute

Put your class in a package and use the future absolute_import. See rationale-for-absolute-imports

Example: the below unittest import will import the unittest of the system library, not the script itself

from __future__ import absolute_import
import unittest

Star

  • Importing all functions of a module. Don't use to get no name resolution conflicts
from module import *
print sqrt(25)

What is the meaning of star ?
The import statement uses the following convention, if a package’s init.py code, the variable all is:

  • defined, its value is taken to be the list of module names. Example with three modules:
__all__ = ["echo", "surround", "reverse"]
  • is not defined, not import all submodules from the package into the current namespace; it only:
    • ensures that the package has been imported (possibly running any initialization code in init.py)
    • imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py.
    • includes any submodules of the package that were explicitly loaded by previous import statements.
import sound.effects.echo
import sound.effects.surround
from sound.effects import *

In this example, the echo and surround modules are imported in the current namespace because they are defined in the sound.effects package when the from…import statement is executed. (This also works when __all__ is defined.)

Doc

Alias

  • Module alias
import moduleName as alias
alias.func() # and not moduleName .funct()
  • Name alias
from module import name as aliasName

The import statement combines two operations;

  • it searches for the named module,
  • then it binds the results of that search to a name in the local scope.

To begin the search, Python needs the fully qualified name of the module (or package)

The name of the module are searched in the searchpath

Searchpath

The path is where Python searches the modules to Import.

Example

  • package structure
spam/
    __init__.py
    foo.py
    bar.py

from .foo import Foo
from .bar import Bar

then executing the following puts a name binding to foo and bar in the spam module:

import spam
spam.foo
<module 'spam.foo' from '/tmp/imports/spam/foo.py'>

spam.bar
<module 'spam.bar' from '/tmp/imports/spam/bar.py'>

Documentation / Reference





Discover More
Card Puncher Data Processing
Object Type - Class

The type of an object is its class. See
Card Puncher Data Processing
Python - Class

in Python The difference with a standard class implementation is that in Python, Classes are objects because the class keyword will creates an object. Only a class object is capable of creating objects. A...
Card Puncher Data Processing
Python - Egg distribution (bdist_egg)

Python Eggs are a python binary distribution format. wheelDoc A “Python egg” is a logical structure embodying the release of a specific version...
Card Puncher Data Processing
Python - Main (Entry Point)

In Python. In the . Example: To provide executable scripts,...
Card Puncher Data Processing
Python - Module Search Path (PYTHONPATH and sys.path)

The path is where Python searches the modules to Import. The current path is automatically included. On Windows add the script dir: PYTHONPATH is an environment variable, that governs the sequence...
Card Puncher Data Processing
Python - Namespace

in Python. The namespace for a module is automatically created the first time a module is imported.
Card Puncher Data Processing
Python Package - Module

On a file system, package are the directories and modules are the files within this directories. A module is a file that contains structure definitions including : variables class and functions....
Card Puncher Data Processing
Python Package - init .py

A regular package is typically implemented as a directory containing an __init__.py file. The __init__.py file is tells Python that you can use the folder to import a module. (The default behavior is...



Share this page:
Follow us:
Task Runner