PYTHONPATH order on Ubuntu 14.04
<p>I have two computers running Ubuntu 14.04 server (let's call them A and B). B was initially a 10.04 but it has received two upgrades to 12.04 and 14.04. I do not understand why the python path is different on the two computers.</p>

<p>As you can see on the two paths below, the pip installation path <code>/usr/local/lib/python2.7/dist-packages</code> comes <strong>before</strong> the apt python packages path <code>/usr/lib/python2.7/dist-packages</code> on Ubuntu A, but it comes <strong>after</strong> on Ubuntu B.</p>

<p>This leads to several problems if a python package is installed both via apt and pip. As you can see below, if both <code>python-six</code> apt package and <code>six</code> pip package are installed, they may be two different library versions.</p>

<p>The installation of packages system is not always my choice, but might be some dependencies of other packages that are installed.</p>

<p>This problem could probably be solved with a virtualenv, but for reasons I will not detail, <strong>I cannot use virtualenv</strong> here, and must install pip packages system-wide.</p>

<h2>Ubuntu A</h2>

<pre><code>&gt;&gt;&gt; import sys, six
&gt;&gt;&gt; sys.path
['',
 '/usr/local/bin',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/local/lib/python2.7/dist-packages/IPython/extensions']
&gt;&gt;&gt; six
&lt;module 'six' from '/usr/local/lib/python2.7/dist-packages/six.pyc'&gt;
</code></pre>

<h2>Ubuntu B</h2>

<pre><code>&gt;&gt;&gt; import sys, six
&gt;&gt;&gt; sys.path
['',
 '/usr/local/bin',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/local/lib/python2.7/dist-packages/IPython/extensions']
&gt;&gt;&gt; six
&gt;&gt;&gt; &lt;module 'six' from '/usr/lib/python2.7/dist-packages/six.pyc'&gt;
</code></pre>

<p>For both machines <code>$PATH</code> is the same, and <code>$PYTHONPATH</code> is empty.</p>

<ul>
<li><p>Why are those PYTHONPATHS different?</p></li>
<li><p>How can I fix the pythonpath order in "Ubuntu B" so it will load pip
packages before the system ones, in a system-wide way? Is there a apt package I should reinstall or reconfigure so the
PYTHONPATH would prioritize pip packages ?</p></li>
</ul>

<p>If you peek into Python's <code>site.py</code>, which you can by opening <code>/usr/lib/python2.7/site.py</code> in a text editor.</p>

<p>The sys.path is augmented with directories for packages distributed within the distribution. Local addons go into <code>/usr/local/lib/python/dist-packages</code>, the global addons install into <code>/usr/{lib,share}/python/dist-packages</code>.</p>

<p>You can change the order by overriding this:</p>

<pre><code>def getsitepackages():
    """Returns a list containing all global site-packages directories
    (and possibly site-python).

    For each directory present in the global ``PREFIXES``, this function
    will find its `site-packages` subdirectory depending on the system
    environment, and will return a list of full paths.
    """
    sitepackages = []
    seen = set()

    for prefix in PREFIXES:
        if not prefix or prefix in seen:
            continue
        seen.add(prefix)

        if sys.platform in ('os2emx', 'riscos'):
            sitepackages.append(os.path.join(prefix, "Lib", "site-packages"))
        elif os.sep == '/':
            sitepackages.append(os.path.join(prefix, "local/lib",
                                        "python" + sys.version[:3],
                                        "dist-packages"))
            sitepackages.append(os.path.join(prefix, "lib",
                                        "python" + sys.version[:3],
                                        "dist-packages"))
        else:
            sitepackages.append(prefix)
            sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
        if sys.platform == "darwin":
            # for framework builds *only* we add the standard Apple
            # locations.
            from sysconfig import get_config_var
            framework = get_config_var("PYTHONFRAMEWORK")
            if framework:
                sitepackages.append(
                        os.path.join("/Library", framework,
                            sys.version[:3], "site-packages"))
    return sitepackages
</code></pre>

python ubuntu ubuntu-14.04 pythonpath ubuntu-server