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/
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 . These are the clean solution. Therefore they are of importance e.g. in HPC environments  in which the Python code has to be distributed in packets prior to execution
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
ln -s /path/to/recent/version/of/module.py module.py
# Or in case your module is an entire directory like
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.
# Assume path to module is