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>The simplest way is to use <a href="https://docs.python.org/2/library/sys.html#sys.path" rel="nofollow">sys.path</a> to make sure that you will have right order of paths added. <code>sys.path</code> gives out the list of paths that are available in the <code>PYHTONPATH</code> environment variable with the right order. If you want any path to have higher priority over others, just add it to the beginning of the list.</p>

<p>And you will also find this in the official docs:</p>

<blockquote>
  <p>A program is free to modify this list for its own purposes.</p>
</blockquote>

<p>WARNING: Even though this gives better control over the priority, just make sure that whatever library you add does not mess with the system libraries. Else, your library will be searched first, as it is in the beginning of the list, and they may replace the system libraries. Just as an example, if you have written a library by the name <code>os</code>, after adding this to the <code>sys.path</code>, that library will be imported instead of Python's in-built. So take all the caution and also with a large amount of salt before jumping to this.</p>

python ubuntu ubuntu-14.04 pythonpath ubuntu-server