Very good guide..
Let’s say you’re in the middle of writing a somewhat large application, and you want to test one of your methods to see if it does what you intended. However, the rest of your application is not yet functional. How do you test the method? You could copy the whole thing into the interpreter and test it that way, or you could copy it into a separate script and run it from the command line. However, both of those methods require also copying various other supporting functions and attributes from elsewhere in your application. What a pain! Instead, you could just import your unfinished application like any other module and run it from the interpreter. Assuming you’ve included a statement like
if __name__==’__main__’: main() before calling the main function in your application, this will work great. If not, you might need to do that first or your application may try to start when you initialize it from the interpreter, depending on how it’s designed. For more about
__name__, see
Using __name__ in Python. Anyway, in order to import our application, we need to add our file(s) to the
PYTHONPATH, which is the list of directories Python searches through to find modules. It is different from your OS’s
Path Environmental Variable. Before we edit our
PYTHONPATH, though, let’s look at what it currently is. I happen to know that your
PYTHONPATH is the same thing as your
sys.path. Let’s see it.
>>> import sys
>>> for i in sys.path:
... print i
...
C:\Python25\Lib\idlelib
C:\Python25\Lib\idlelib
C:\Python25\lib\site-packages\mp3play-0.1.15-py2.5.egg
C:\Windows\system32\python25.zip
C:\Python25\DLLs
C:\Python25\lib
C:\Python25\lib\plat-win
C:\Python25\lib\lib-tk
C:\Python25
C:\Python25\lib\site-packages
C:\Python25\lib\site-packages\PIL
C:\Python25\lib\site-packages\win32
C:\Python25\lib\site-packages\win32\lib
C:\Python25\lib\site-packages\Pythonwin
C:\Python25\lib\site-packages\wx-2.8-msw-unicode
Altering PYTHONPATH in Windows: Method 1 (append to sys.path)
The first method is intuitive. Look at our
sys.path above. in the example
above. Why not just add to it from the interpreter? Let’s say you want to add the directory
C:\Users\Public\Programs.
>>> sys.path.append('C:\\Users\\Public\\Programs')
>>> for i in sys.path:
... print i
...
C:\Python25\Lib\idlelib
C:\Python25\Lib\idlelib
C:\Python25\lib\site-packages\mp3play-0.1.15-py2.5.egg
C:\Windows\system32\python25.zip
C:\Python25\DLLs
C:\Python25\lib
C:\Python25\lib\plat-win
C:\Python25\lib\lib-tk
C:\Python25
C:\Python25\lib\site-packages
C:\Python25\lib\site-packages\PIL
C:\Python25\lib\site-packages\win32
C:\Python25\lib\site-packages\win32\lib
C:\Python25\lib\site-packages\Pythonwin
C:\Python25\lib\site-packages\wx-2.8-msw-unicode
C:\Users\Public\Programs
It worked! However, there is one shortfall to this method of editing the
PYTHONPATH: it is short-lived. As soon as you close the interpreter, your
PYTHONPATH returns to its previous state. This could be a good thing, but if you plan to import from your modules often, you should try one of the other methods.
Altering PYTHONPATH in Windows: Method 2 (with a .pth file)
The second method is also fairly simple. You just create a text file, write the directory path you want to add (e.g.
C:\\Users\\Public\\Programs), save it as
<AnyFileName>.pth, and place it in your site-packages directory (you can see that mine is at
C:\Python25\lib\site-packages). When you install modules, this is how they add their directories to your
PYTHONPATH, and you can see that a few modules have done exactly that in my installation (mp3play, PIL, Pythonwin, win32, etc.).
Altering PYTHONPATH in Windows: Method 3 (With an Environmental Variable)
The third method takes the most work and might seem more permanent, but in reality it’s not that hard to do, and it’s easy to change. In Windows, go to your Environmental Variables editor (Control Panel -> System -> Advanced System Settings -> Environmental Variables). First, look for PYTHONPATH. It’s probably not in there unless you’ve already done this. So click on
New, and type PYTHONPATH. Now enter the directory(s) you want to add. Make sure to use the forward-slash format Windows likes (
C:\Users\Public\Programs), place a semicolon between directories if you add more than one, do not leave spaces between entries, and do not place a semicolon after the last directory on the list! When you’re finished, your directory will be ADDED to your
PYTHONPATH. There is no need to specify those directories that are already in your
PYTHONPATH. Similarly, if you’ve added directories using the second method, they will all still be available after you create the PYTHONPATH Environmental Variable even if you don’t include them in it.
NOTE: If you’ve edited your
PYTHONPATH using the second or third methods, you will not see the changes reflected in your
sys.path until you’ve restarted the interpreter!
Altering PYTHONPATH in Ubuntu: Method 1 (append to sys.path)
There are four methods to alter the PYTHONPATH in Ubuntu. The first one is identical to Method 1 in Windows: just append to sys.path. See the description for Windows above for details.
Altering PYTHONPATH in Ubuntu: Method 2 (with a .pth file or symbolic link)
The second method is similar to Method 2 for Windows: place the new path in a
.pth file. However, in Ubuntu after Python2.5, the site-packages directory is no longer in the PYTHONPATH by default. Instead, you need to put your
.pth file in the dist-packages directory (
/usr/local/lib/python2.6/dist-packages). Instead of using a
.pth file in that directory, you could also simply create a symbolic link to the dist-packages directory like this:
$ sudo ln -s /home/greeenguru/Programs/example /usr/local/lib/python2.6/dist-packages/example
Altering PYTHONPATH in Ubuntu: Method 3 (temporarily alter PYTHONPATH)
The third method is to
temporarily alter the PYTHONPATH Environmental Variable:
$ export PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH
Note that in Linux, paths are separated with a colon whereas in Windows they’re separated with a semicolon. That last part will ensure that the PYTHONPATH Variable is appended to rather than overwritten. This modification only lasts as long as the current shell session.
Altering PYTHONPATH in Ubuntu: Method 4 (permanently alter PYTHONPATH)
In order to permanently alter the PYTHONPATH in Ubuntu, you have to add the export command from the previous method to two of your config files that reside in your home directory:
.bashrc and
.profile.
.bashrc will ensure the PYTHONPATH is updated for any python application running through BASH, and
.profile should cover the rest. Just to be sure, though, let’s take a look at the PYTHONPATH before and after:
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
Ok, so that’s the PYTHONPATH before alteration. Now let’s update our
.bashrc:
$ echo -e '# Add a directory to PYTHONPATH\nexport PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH' | tee -a ~/.bashrc
After this, you need to close your terminal and open a new one to ensure your new .bashrc is read. Now let’s take a look after updating the
.bashrc:
['', '/home/greeenguru/Programs/example', '/home/greeenguru', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
Notice that now my home directory and my example directory are both in the PYTHONPATH. But what happens if I try to run the Python Interpreter through IDLE (a Python Shell that doesn’t start through BASH)?:
['/home/greeenguru', '/usr/bin', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
My home directory is there for some reason, but my example directory definitely didn’t make it. Now let’s try updating the
.profile config file:
$ echo -e '# Add a directory to PYTHONPATH\nexport PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH' | tee -a ~/.profile
Your
.profile config file is only read when you log in, as far as I know, so you have to log out and log back in again for the change to take effect. Here’s what the PYTHONPATH looks like in IDLE after logging back in:
['/usr/bin', '/home/greeenguru/Programs/example', '/home/greeenguru', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
It worked!