Python uses the directories listed in PYTHONPATH to find a module. How can I assure that the module I am importing is on this list of paths?

Recommended Answers

All 3 Replies

This will show the PYTHONPATH:

# show the system path for Python = PYTHONPATH

import sys
print sys.path

# notice that your present working folder is 
print sys.path[0]

To add another path, the one with your module perhaps, do this at the beginning of your program:

# let's say your module is at D:\Python24\Examples\PP2E\Dbase\TableBrowser\guitools.py
 
import sys

# then append to PYTHONPATH  the location of \PP2E
# so can use eg. from PP2E.Dbase.TableBrowser.guitools import * 
sys.path.append(r'D:\Python24\Examples')

Does PP2E refer to the examples in the book "Programming Python 2nd Edition" by Mark Lutz? I just borrowed a copy from a friend. This book spends a lot of space on the Tkinter GUI. I am a little frustrated with the inadequate index.

Ene,

clever assumption! You are right, the samples won't work unless you let Python know where some of the modules are located on your drive with the second option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.