I have a python application that depends on the uuid module, but on the server where I need to run it, uuid is not installed. I do not have root on the server so I cannot place the uuid .pys in the /usr/lib/python2.4/site-packages folder... is there a way that I can utilize the .py's from elsewhere? like my ~ ?
views:
106answers:
5Is it possible to utilize a python module that isnt installed into the python directories in linux?
There are several ways to do this. The fastest is the simple command:
export PYTHONPATH=path/to/module/directory
Alternatively, you can use virtualenv. Just sudo apt-get install python-virtualenv (?). It's a very common development tool used for using modules that you don't necessarily want installed in your local Python installation.
Yes, there's no need to install most Python modules. uuid.py is simple enough you don't need to build or install it at all. Just download it, unpack it, and place the uuid.py file in your directory with your code. "import uuid" will work (the current working directory is in the Python path). This hack works fine up until you are doing serious application deployment management.
BTW, I believe the uuid module is installed already with Python 2.5 and above.
Python looks for modules to import by means of the sys.path
variable. You can change this in your program to point to new modules your program needs. By default this will include the programs own directory as well as the python system directories, but you can certainly add just about anything to it.
To follow up on this in the future this is addressed in [PEP 370][1] a good blog article explain how it works here [http://jessenoller.com/2009/07/19/pep-370-per-user-site-packages-and-environment-stew/][2].
To install packages you can as well have a look to [virtualenv][3] and [pip][4] which give you the ultimate way to have a clean environment.
If it's a single module I would consider including it on my project path. If it's something more complex (like a package, binary files, etc) and I don't want to modify the project sys.path (for example because it's the source of Django and I don't want to mess with updates) I install the package somewhere and then I add the path to a .pth file on my project directory (the current directory is always on the Python Path.) This way you don't have to be playing with your PYTHONPATH or project sys.path.
You can check the format of the pth files here:
http://bob.pythonmac.org/archives/2005/02/06/using-pth-files-for-python-development/