Setting Python Path

From Akionux-wiki
Share/Save/Bookmark
Jump to: navigation, search

Abstract

Setting Path environment on Python(PYTHONPATH).

Environment

  • Python 2.7.5
  • Slackware Linux 14.1

Displaying PYTHONPATH

import sys and display sys.path on python command line.

% python      (git)-[master]
Python 2.7.5 (default, May 29 2013, 02:28:51) 
[GCC 4.8.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode']

Setting PYTHONPATH

By environment variable

Add following line to ~/.bashrc[1].

export PYTHONPATH="${PYTHONPATH}:/my/other/path1:/my/other/path2"

By Python code

Append:

>>> import sys
>>> sys.path.append('/my/other/path')
>>> sys.path.append(['/my/other/path1','/my/other/path2'])

Remove:

>>> import sys
>>> sys.path.remove('/my/other/path')
>>> sys.path.remove(['/my/other/path1','/my/other/path2'])

References

  1. python - Permanently add a directory to PYTHONPATH - Stack Overflow http://stackoverflow.com/questions/3402168/permanently-add-a-directory-to-pythonpath