Thursday, December 27, 2012

Adding New Path to PYTHONPATH

If you've made a module in python, chances are, you would like to be able to import the module into one of your python script. However, if you don't want to place your script in the "standard" python location, you have to add the path of the directory containing your module to PYTHONPATH environment variable. You can check the value of the PYTHONPATH like this:
me@mymachine ~/work $ python
Python 2.7.3rc2 (default, May  6 2012, 20:02:25)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import  sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/home/pi/work/c_to_python', '/usr/lib/pymodules/python2.7']
This link explains the alternatives to do that. I choose the easiest path, i.e. adding a *.pth file to one of the path already parsed by python. For example, adding my_path.pth to /usr/lib/python2.7/dist-packages directory, with the content of my_path.pth as follows:
This of course assume that your python module is in /home/me/my_python_lib_dir. That's it, hopefully this could be useful for those wanting to place a python module in "non-standard" python path(s).
Post a Comment

No comments: