After the path is added, you can import the needed module in the added production directory.
LISTING 13.14 Adding a New Directory to the Path
Click here to view code image
>>> import sys
>>> sys.path.append('/usr/local/lib/python3.2/site-packages')
>>>
>>> sys.path
['', '/usr/lib/python3.2', '/usr/lib/python3.2/plat-linux2',
'/usr/lib/python3.2/lib-dynload',
'/usr/local/lib/python3.2/dist-packages',
'/usr/lib/python3/dist-packages',
'/usr/local/lib/python3.2/site-packages']
>>>
>>> import arith
>>>
As you can see in Listing 13.14, with the site-packages directory now added to the path, Python
can find the arith module with no problems.
Keep in mind that the path resets back to its default every time your script finishes or you exit the
Python interactive shell. This means you need to add the new path every time you want to import the
new module.
Did You Know: The Python Path
You Linux gurus should know that there is an environment variable called
PYTHONPATH that you can modify to include your new custom modules directory. By
changing this environment variable and adding it to an environment file, you make the
path permanently include the custom modules directory. There is then no need to use
the sys module to change your path every time.
Testing the Production Custom Module
Testing a production custom module is optional. However, it is always a good idea to test your
module’s functions after you have moved it to the production directory.
LISTING 13.15 Testing the Production arith Module
>>> import arith
>>> arith.dbl(20)
40
>>> arith.addem(7,35)
42
>>>
>>>
As you can see in Listing 13.15, the arith module is imported with no problem. Both the .dbl
method and the .addem method run flawlessly.