How To override Import order of Python
As it is commonly known the Python interpreter searches in a list of directories for a module. This list looks in most cases something like this:
- Python installation directory containing built-in modules
- Internal list variable sys.path
- Current working directory
- Directory containing system-wide installed modules (by root user), e.g. /usr/lib/python2.6/
- Directory containing modules installed by users without root privileges, e.g. /usr/local/lib/python2.6/
Problem
In some cases you can run into the problem of having an old version of a system-wide installed module, but you need a more recent version and you do not have root privileges.
In this situation one has basically two options:
- Using a very dirty hack, which I will outline on this page. This one is dirty, but fool proof.
- Using relative import statements [2]. These are the clean solution. Therefore they are of importance e.g. in HPC environments [1] in which the Python code has to be distributed in packets prior to execution
Solution
In this situation you have two options:
- Make a symbolic link in the directory containing your script to the directory containing the recent version of the module, e.g.
# Assume we are in the directory containing
# the main script file.
# Assumepath to module is
# /path/to/recent/version/of/module.py
ln -s /path/to/recent/version/of/module.py module.py
# Or in case your module is an entire directory like
# /path/to/recent/version/of/
ln -s /path/to/recent/version/of of
- Before you import the problematic module in your script insert the directory containing the more recent version at position 0 to the sys.pathvariable, e.g.
import sys
# Assume path to module is
# /path/to/recent/version/of/module.py
sys.path.insert(0,"/path/to/recent/version/of")
import module
References
- http://www.science-computing.de/fileadmin/downloads/Solutions/Workflow_Management/flowGuide_whitepaper_unternehmenseinsatz_de.pdf
- http://legacy.python.org/dev/peps/pep-0328/