Appending to Your Python Path

Question

How do you append directories to your Python path?

Answer

Your path (i.e. the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module. Since path is a list, you can use the append method to add new directories to the path.

For instance, to add the directory /home/me/mypy to the path, just do:

import sys
sys.path.append("/home/me/mypy")

Greg Ward's Installing Python Modules document has more information on other ways of modifying the search path.

My Python Book Cover Image Advertisement: In 2012, I published the book A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences. If you're new to Python, you may find it useful!

Return to the Tips and Examples index page.

Updated: February 19, 2004 by Johnny Lin <email address>. License.