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:

  1. Python installation directory containing built-in modules
  2. Internal list variable sys.path
    1. Current working directory
    2. Directory containing system-wide installed modules (by root user), e.g. /usr/lib/python2.6/
    3. 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:

  1. Using a very dirty hack, which I will outline on this page. This one is dirty, but fool proof.
  2. 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


In this situation you have two options:

  1. 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/ ln -s /path/to/recent/version/of/ # Or in case your module is an entire directory like # /path/to/recent/version/of/ ln -s /path/to/recent/version/of of
  2. 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/
    import module