modules part 1

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

modules part 1

Postby metulburr » Tue Jun 04, 2013 6:11 pm

Each file is a module, and modules import other modules to use the names they define. Modules provide an easy way to organize components into a system by serving as self-contained packages of variables known as namespaces.

let's a client (importer) fetch a module as a whole
Code: Select all
import tester

allows clients to fetch particular names from a module
Code: Select all
from tester import Klass

allows clients to fetch a module from a different directory. (see Relative Imports on Modules Part 3)
Code: Select all
from .. import tester #import the module tester from parent directory

allows an imported name to be given a different name in your script
Code: Select all
from tester import Klass as Kl
import tester as test

Provides a way to reload a module's code without stopping Python
Python2.x (reload is a built-in)
Code: Select all

Python3.x (reload has been moved to the imp module)
Code: Select all
from imp import reload

How it works:
Modules import without the suffix .py. Imported modules performs a 3-step process the first time it is imported:

1)Find the modules file
Python first looks at the home directory that the file is located in:
Code: Select all

Using the previous example, in if you wanted to import the module, in you would have the following syntax:
Code: Select all
import a

If a program is located entirely in a single directory, all of it's imports will work automatically with no path configuration required. Because this directory is searched first, its files will also override modules of the same name in directories elsewhere on the path.

Python then searches your PYTHONPATH directories:
PYTHONPATH is a list of user-defined and platform-specific names of directories that contain python code files. You can add all the directories from which you wish and python will extend the modules search path to inlcude all the directories in your PYTHONPATH lists. This setting is only important when you are importing files across directory boundaries.
Code: Select all
import sys

Python then searches your standard library directories:
These are always searched and do not need to be added to your PYTHONPATH

Python then searches for directories in your .pth files:
This is a file you create that allows you to add directories to the module search path by listing them, one per line, in a text file whose names ends with .pth.

You can always inspect the modules search path
Code: Select all
import sys

sys.path is the merging of the home directory of the top-level file (or an empty string to designate the current working directory), any PYTHONPATH directories, the contents of any .pth files path you created, and the standard library directories.

Some changes last only the duration of the script, but PYTHONPATH and .pth files allow for more permanent ways to modify the path.

2) Compile it to byte code (if needed)
After finding the the source code file that matches the import statement, Python next compiles it to byte code if necessary. Python checks the file timestamps and if the byte code file is older than the source file (ie, if you changed the source), automatically regenerates the byte code when the program is run. If on the other hand, it finds a .pyc byte code file that is not older than the corresponding .py source file, it skips thie source-to-byte code compile step. IF python finds only a byte code file on the search path and no source, it loads the byte code directly (meaning you can ship your a program as jsut byte code files and avoid sending source). The compile step is bypassed if possible to speed program startup.

Only imported files leave behind a .pyc file

3) Run the modules code to build the objects it defines
The final step is is it executes the byte code of the module. All statements in the file are executed in trun, from top to bottom, and anyassignments made to names during this step generate attributes of the resulting module object.

Because this step actually runs the file's code, if any top-level code in a module does real work, you will see the results at import time. Because of this, any given module is imported only once per process by default. If you need to import a file again after it it has been loaded, you have to force the issue with imp.reload.

3rd party software
3rd party software use distutils from the standard library to install themselves. So no path configuration is required to use thier code. These typically come with a file

Basic structure example using linux terminal
I did this all on the terminal so you can actually see what i write to the file. For quick rundown: mkdir creates a directory, touch creates a file, cat prints off the file content, ls lists the current directory content, echo "some_string" >> filename writes some_string to the file filename.

this is an example all on the terminal:
Code: Select all
metulburr@arch ~ $ mkdir example
metulburr@arch ~ $ cd example
metulburr@arch ~/example $ touch
metulburr@arch ~/example $ ls
metulburr@arch ~/example $ echo "from subfolder import module" >>
metulburr@arch ~/example $ echo "print('running main')" >>
metulburr@arch ~/example $ cat
from subfolder import module
print('running main')
metulburr@arch ~/example $ mkdir subfolder
metulburr@arch ~/example $ ls  subfolder
metulburr@arch ~/example $ touch subfolder/
metulburr@arch ~/example $ touch subfolder/
metulburr@arch ~/example $ echo "print('running module')" >> subfolder/
metulburr@arch ~/example $ cat subfolder/
print('running module')
metulburr@arch ~/example $ python3
running module
running main
metulburr@arch ~/example $ python2
running module
running main
metulburr@arch ~/example $

and here is what each line is doing:
Code: Select all
create directory example in ~/
cd directory into example
create a new file called
list files in directory (ls)
write to the file on first line in "from subfolder import module"
write to the file on second line in "print('running main')"
print the content of what is in (cat)
make directory subfolder
list files and directories in current directory "example"
create a new file in  subfolder called
create a new file in subfolder called
write to the file on first line in the subfolder directory to file
print the content of what is in subfolder/
execute via (python in the directory example
(code can be python2.x or 3.x)

the structure:
  • /home/username/example:
    • subfolder

go to Modules Part 2
we will be moving to on October 1 2016
more details here
User avatar
Posts: 2226
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