Skip to content Skip to sidebar Skip to footer

Making Python Use Code In My Directory (not That In /usr/...)

I am trying to work on a Python library that is already installed on my (Ubuntu) system. I checked out that library, edited some files, and wrote a small script to test my changes.

Solution 1:

You can dictate where python searches for modules using the PYTHONPATH environment variable:

When a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names. When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation-dependent default path; on Unix, this is usually .:/usr/local/lib/python.

from http://docs.python.org/tutorial/modules.html#the-module-search-path

Solution 2:

Lets consider a more general issue. In the /usr/share/pyshared/ there are lots of modules. You wish to override just one of the modules. Say the module name is xyz.py. And it happens to use other modules in /usr/shared/pyshared also.

Say we create $HOME/mylibs and add $HOME to Python's sys.path.

Now wherever we have to use xyz, we do something like

from mylibs import xyz

If we wish to revert back to the original xyz, we try:

import xyz # picks up from /usr/shared/pyshared 

I wonder if this kind of approach would be more general. You mask only those modules which you are overriding and keep others in use as usual.

Solution 3:

import sys
from os.path import join, dirname, pardir

sys.path.insert(0, join(dirname(__file__), pardir))

This will check the src directory for any python modules, and will look there first. So even if you have a module with the same name installed elsewhere, this will cause python to load the local one.

sys.path documentation.

Solution 4:

Check the complete path that Python uses through sys.path. You should be able to add to the path (in front for precedence).

Of course, you can also use PYTHONPATH environment variable or work-around using .pth files.

Solution 5:

It might work if you set the PYTHONPATH environment variable to include that directory, e.g.:

$ cd src$ export PYTHONPATH=./library_package$ python my_package/my_script.py

Post a Comment for "Making Python Use Code In My Directory (not That In /usr/...)"