modules part 3

A place where you can post Python-related tutorials you made yourself, or links to tutorials made by others.

modules part 3

Postby metulburr » Wed Jun 05, 2013 5:28 pm

Package Imports
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 (and the from statement indicates that has a Class named Klass, of which it imports). This indicates the directory structure in windows:

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. In Python 2.x, Each directory named within the path of a package import statement must contain a file named, or your package import will fail. For example, dir1 and dir2 must both contain a file named, the container dir0 does not because its not listed in the import statement. The structure would look like this:
Code: Select all

These 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 file. These files are a place to put code to initialize the state required by files in a package.

The 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 ../ 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. More examples can be found here

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
>>> s = 'sys'
>>> 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 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
>>> name = 'sys'
>>> 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
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
User avatar
Posts: 2120
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest