Package imports are nothing more than a directory of imports. It turns a path on your computer into another namepsace, with attributes corresponding to the subdirectories and module files that the directory contains.
- Code: Select all
import dir1.dir2.mod
from dir1.dir2.mod import Klass
Here a directory named dir1, with a subdirectory named dir2, which in that has a module named mod.py (and the from statement indicates that mod.py has a Class named Klass, of which it imports). This indicates the directory structure in windows:
dir0\dir1\dir2\mod.py
The directory dir0 needs to be added to your module's search path (unless it's home directory of the top-level file).
You cannot use platform-specific path syntax, but only dotted (.) seporators. Each directory named within the path of a package import statement must contain a file named __init__.py, or your package import will fail. For example, dir1 and dir2 must both contain a file named __init__.py, the container dir0 does not because its not listed in the import statement. The structure would look like this:
- Code: Select all
dir0\
dir1\
__init__.py
dir2\
__init__.py
mod.py
These __init__.py files can contain code, but they can be completely emtpy. These serve to prevent directories with common names from unintentionally hiding true modules that appear later on the module search path. Without this safe guard, python might pick a directory that has nothing to do with your code. The first time python imports through a directory, it automatically runs all the code in the directory's __init__.py file. These files are a place to put code to initialize the state required by files in a package.
The __init__.py file is not required in Python3.x. Only Python2.x
Relative Imports
in Python 2.7 > and 3.x, you can use leading dots (.) to specify that they require modules located within the same package. This feature applies to only the from statement and only applies to imports within it's package.
in Python 2.6 < to apply this feature requires:
- Code: Select all
from __future__ import absolute_import
- Code: Select all
from . import mod
means import a module named mod located in the same package directory as the file in which this statement appears
- Code: Select all
from .mod import Klass
means from a module named mod located in the same package as the file that contains this statement, import the class Klass. This is a package import.
An import wihtout a leading dot always causes Python to skip the relative components of the module import search path and look instead in the absolute directories that sys.path contains.
- Code: Select all
from .. import mod
equivalent to ../mod.py so to speak, from parent directory import module named mod
Mixed Usage __name__ and '__main__'
This is a trick to let you both import a file as a module and run it as a standalone program. Each module has a built-in attribute called __name__ which python sets as:
If the file is being run as a top-level program file, __name__ is set to the string "__main__" when it starts
If the the file is being imported instead, __name__ is set to the module's name as known by it's clients
It basically tests its own __name__ to see if it is being imported or ran.
The syntax:
- Code: Select all
if __name__ == '__main__':
#put code here
Code in the if block will only execute if this file is being ran, not when this file is being imported.
Importing by string
Sometimes you will need to get the name of the module to be imported as a string, in which case, you cannot:
- Code: Select all
>>> import 'sys'
File "<stdin>", line 1
import 'sys'
^
SyntaxError: EOL while scanning string literal
or you cannot:
- Code: Select all
>>>>> import s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 's'
Python expects a varaible name for the first one and python atttmpts to import s.py on the second one. To account for this you can use the special tool to load a module dynamically from a string that is generated at runtime exec(). exec() compiles a string of code and passes it to python interpreter to be executed.
- Code: Select all
>>>>> exec('import {}'.format(name))
>>> sys
<module 'sys' (built-in)>
The other method is to use the built-in __import__ function, in which it returns the modules object
- Code: Select all
>>> sys = __import__('sys')
>>> sys
<module 'sys' (built-in)>
Naming your modules
You can name your modules anything you want. Howver if you name your modules the same name as modules in the standard library. You will then importing your module and not the standard library's module. For example if i name a module ipaddress, os, sys, etc. Then by import os, or whatever, etc. I am importing my module instead of the standards library. Rename your module to something not in the standard library.
You can get a list of modules with
- Code: Select all
>>> help('modules')
Go to Modules Part 2
This tutorial was based off of Learning Python 4th Edition by Mark Lutz