1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Putting It All Together With Syntax

From , former About.com Guide

10 of 10

Modules - Part 2: Importing Functions From Your Own Programs

Whenever you write a program that contains functions (which is almost any program of substantial size and purpose), you can call on the functions of that program from others. This assumes that the programs do not have the same names as Python modules. When the Python interpreter is told to import a module, it checks both the location(s) of the Python library and the current directory in order to find the requested module.

The precise order of this checking is determined by the import search path, a list of directories contained in sys.path. You can view the path in the Python shell as follows:

>>> import sys
>>> sys.path
['', '/usr/lib/python2.4/site-packages/TurboGears-0.8.9-py2.4.egg', '/usr/lib/python2.4/site-packages/TestGears-0.2-py2.4.egg', '/usr/lib/python2.4/site-packages/FormEncode-0.4-py2.4.egg', '/usr/lib/python2.4/site-packages/cElementTree-1.0.5_20051216-py2.4-linux-i686.egg', '/usr/lib/python2.4/site-packages/elementtree-1.2.6-py2.4.egg', '/usr/lib/python2.4/site-packages/json_py-3.4-py2.4.egg', '/usr/lib/python2.4/site-packages/SQLObject-0.7.1dev_r1457-py2.4.egg', '/usr/lib/python2.4/site-packages/CherryPy-2.1.1-py2.4.egg', '/usr/lib/python2.4/site-packages/kid-0.8-py2.4.egg', '/usr/lib/python2.4/site-packages/setuptools-0.6a11-py2.4.egg', '/usr/lib/python2.4/site-packages/Django-0.91-py2.4.egg', '/usr/lib/python24.zip', '/usr/lib/python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk', '/usr/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages/HTMLgen', '/usr/lib/python2.4/site-packages/Numeric', '/usr/lib/python2.4/site-packages/PIL', '/usr/lib/python2.4/site-packages/gtk-2.0', '/usr/lib/site-python']

Note the first empty item in the list, ''. This equates to the directory in which Python is executing the program. With this first, Python will search the current directory before all others.

Other tutorials in this series: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8

Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.