Last modified on 5 April 2014, at 15:58

Python Programming/Modules

Modules are a simple way to structure a program. Mostly, there are modules in the standard library and there are other Python files, or directories containing Python files, in the current directory (each of which constitute a module). You can also instruct Python to search other directories for modules by placing their paths in the PYTHONPATH environment variable.

Importing a ModuleEdit

Modules in Python are used by importing them. For example,

import math

This imports the math standard module. All of the functions in that module are namespaced by the module name, i.e.

import math
print math.sqrt(10)

This is often a nuisance, so other syntaxes are available to simplify this,

from string import whitespace
from math import *
from math import sin as SIN
from math import cos as COS
from ftplib import FTP as ftp_connection
print sqrt(10)

The first statement means whitespace is added to the current scope (but nothing else is). The second statement means that all the elements in the math namespace is added to the current scope.

Modules can be three different kinds of things:

  • Python files
  • Shared Objects (under Unix and Linux) with the .so suffix
  • DLL's (under Windows) with the .pyd suffix
  • directories

Modules are loaded in the order they're found, which is controlled by sys.path. The current directory is always on the path.

Directories should include a file in them called __init__.py, which should probably include the other files in the directory.

Creating a DLL that interfaces with Python is covered in another section.

Creating a ModuleEdit

From a FileEdit

The easiest way to create a module by having a file called mymod.py either in a directory recognized by the PYTHONPATH variable or (even easier) in the same directory where you are working. If you have the following file mymod.py

class Object1:
        def __init__(self):
                self.name = 'object 1'

you can already import this "module" and create instances of the object Object1.

import mymod
myobject = mymod.Object1()
from mymod import *
myobject = Object1()

From a DirectoryEdit

It is not feasible for larger projects to keep all classes in a single file. It is often easier to store all files in directories and load all files with one command. Each directory needs to have a __init__.py file which contains python commands that are executed upon loading the directory.

Suppose we have two more objects called Object2 and Object3 and we want to load all three objects with one command. We then create a directory called mymod and we store three files called Object1.py, Object2.py and Object3.py in it. These files would then contain one object per file but this not required (although it adds clarity). We would then write the following __init__.py file:

from Object1 import *
from Object2 import *
from Object3 import *
 
__all__ = ["Object1", "Object2", "Object3"]

The first three commands tell python what to do when somebody loads the module. The last statement defining __all__ tells python what to do when somebody executes from mymod import *. Usually we want to use parts of a module in other parts of a module, e.g. we want to use Object1 in Object2. We can do this easily with an from . import * command as the following file Object2.py shows:

from . import *
 
class Object2:
        def __init__(self):
                self.name = 'object 2'
                self.otherObject = Object1()

We can now start python and import mymod as we have in the previous section.

External linksEdit