How can I edit the PYTHONPATH environmenal variable in Linux?

Brad Hards bhards at bigpond.net.au
Tue Jan 7 11:17:32 CET 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 6 Jan 2003 01:13, Russ Chaffin wrote:
> Can someone show how to edit the PYTHONPATH environmental variable in
> Linux? I'm a novice in Linux so my learning curve is pretty steep at this
> point. I'm using a debian disto of Linux 2.4. I'm also using a bash shell
> to do my editing.
You modify environmental variables with a simple assignment:

Lets do this in stages.

First: PYTHONPATH not set:
bradh at squirt bradh $ python
Python 2.2.1 (#1, Oct  4 2002, 09:50:49)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.2', '/usr/lib/python2.2/plat-linux2', 
'/usr/lib/python2.2/lib-tk', '/usr/lib/python2.2/lib-dynload', 
'/usr/lib/python2.2/site-packages']
>>>

And now with the path set:
bradh at squirt bradh $ PYTHONPATH="/lib" python
Python 2.2.1 (#1, Oct  4 2002, 09:50:49)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/lib', '/usr/lib/python2.2', '/usr/lib/python2.2/plat-linux2', 
'/usr/lib/python2.2/lib-tk', '/usr/lib/python2.2/lib-dynload', 
'/usr/lib/python2.2/site-packages']
>>>

Note that the next time you run, this won't have any effect:
bradh at squirt bradh $ python
Python 2.2.1 (#1, Oct  4 2002, 09:50:49)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.2', '/usr/lib/python2.2/plat-linux2', 
'/usr/lib/python2.2/lib-tk', '/usr/lib/python2.2/lib-dynload', 
'/usr/lib/python2.2/site-packages']

The reason for this is related to the scope in which an environmental variable 
exists in shell script, and isn't very important. What is important is 
knowing that if you want to have PYTHONPATH set everytime, you probably want 
to use the "export" keyword. Here is an example:
bradh at squirt bradh $ export PYTHONPATH='/usr/local/lib'
bradh at squirt bradh $ python
Python 2.2.1 (#1, Oct  4 2002, 09:50:49)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib', '/usr/lib/python2.2', '/usr/lib/python2.2/plat-linux2', 
'/usr/lib/python2.2/lib-tk', '/usr/lib/python2.2/lib-dynload', 
'/usr/lib/python2.2/site-packages']
>>>

You can remove the export with the -n (conceptually, not export, or negate 
export) option:
bradh at squirt bradh $ export -n PYTHONPATH
bradh at squirt bradh $ python
Python 2.2.1 (#1, Oct  4 2002, 09:50:49)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.2', '/usr/lib/python2.2/plat-linux2', 
'/usr/lib/python2.2/lib-tk', '/usr/lib/python2.2/lib-dynload', 
'/usr/lib/python2.2/site-packages']
>>>

Trust that this solves your problems.

If you want to avoid typing the export PYTHONPATH part in each time you log 
in, you can add it to a file that gets run on login, usually ~/.bashrc

Brad
- -- 
http://linux.conf.au. 22-25Jan2003. Perth, Aust. I'm registered. Are you?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE+Gqk8W6pHgIdAuOMRAosQAJ9YzJbrDnjo6vgPzTdpdTNWlQeYugCeLsIo
e8SF3o6iNudD7kcSiBmGsV8=
=+AcI
-----END PGP SIGNATURE-----






More information about the Python-list mailing list