The import search path in Python

As a python programmer, i feel it necessary to mention about the library search path in python. Python looks in several places in your system when we try and import a module in python.
Precisely , it searches in all directories mentioned in sys.path. Just try and see the directories in sys.path.
>>>import sys
>>>sys.path
This returns you a list of the directories where python does a ‘look in’ to import your module. This list may vary from system to system and depends on the python version that you use.For me, sys.path is :

[”, ‘/usr/lib/python2.5’, ‘/usr/lib/python2.5/plat-linux2’, ‘/usr/lib/python2.5/lib-tk’, ‘/usr/lib/python2.5/lib-dynload’, ‘/usr/local/lib/python2.5/site-packages’, ‘/usr/lib/python2.5/site-packages’, ‘/usr/lib/python2.5/site-packages/Numeric’, ‘/usr/lib/python2.5/site-packages/PIL’, ‘/usr/lib/python2.5/site-packages/gst-0.10’, ‘/var/lib/python-support/python2.5’, ‘/usr/lib/python2.5/site-packages/gtk-2.0’, ‘/var/lib/python-support/python2.5/gtk-2.0’]

A few points about sys.path:

Importing the sys module makes all the attributes and functions of the module available to you.

sys.path as i mentioned gives you the current search path.

You could add a new directory to the python path by appending the list ‘sys.path’
sys.path.append(/my_directory/my_setup)

Again view sys.path and you will find the directory mentioned above in the new search path.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s