Friday 13 September 2013

How to install your own python modules

Problem: You have python code, such as a library, that you want available for other programs you, or others, may be developing. You also may want to add your code into a debian or rpm package for easy distribution.


Solution: Add a [name].pth file and your package to /usr/local/lib/pythonX.Y/dist-packages.


Important Notes:
  • site-packages is no longer used. Read the site.py module for more information on this.
  • The [name].pth contains a relative or absolute path to the directory or specific python files you want added to the interpreter, each item on its own line. (Comments prefixed with # are also recognized)

So I have this example:

/usr/local/lib/python3.2/dist-packages/mylib.pth
    # This file contains path to my library (relative):
    # Adds every .py file in mylib/ folder
    mylib/
    mylib/static/mylibvars.py



The path /usr/local/lib/python3.2/dist-packages/mylib/ contains library files and /usr/local/lib/python3.2/dist-packages/mylib/static/mylibvars.py is just to show how to specify an individual file.


Once you open up your interpreter, type in the following commands to check your work.


>>> import sys
>>> print(sys.path)