How can I import a different version of a python module?
<p>I need to run my python script under sklearn v0.17 and on the server they have sklearn v0.15 installed.</p>

<p>So I downloaded the <code>scikit-learn-0.17</code> package into <code>/home/mydir/lib/python2.7/site-packages/</code> and installed the package.</p>

<p>However when I goto other directories and tried to run python and <code>import sklearn</code> the version is still 0.15.</p>

<p>I created <code>~/.startup.py</code> and put the following code</p>

<pre><code>import sys  
sys.path.insert(0,"/home/mydir/lib/Python2.7/site-packages/")
</code></pre>

<p>then I pointed to <code>~/.startup.py</code> in <code>~/.bashrc</code> with </p>

<pre><code>PYTHONSTARTUP=~/.startup.py
</code></pre>

<p>But it does not help.</p>

<p>I am wondering how to fix this. Thank you!</p>

<p>The following files/dirs are in <code>site-packages/</code></p>

<pre><code>easy-install.pth              scikit-learn-0.17         site.pyc
pysam-0.9.1.4                 scikit_learn-0.17-py2.7.egg-info  site.pyo
pysam-0.9.1.4-py2.7-linux-x86_64.egg  site.py               sklearn
</code></pre>

<p>Python Virtual Environments were made to fix this problem. Create a virtual environment by navigating to the directory of your project and enter the <code>pyvenv ./Env</code> command. Activate the environment on a linux system with <code>source ./Env/bin/activate</code>. Now you have a sandboxed python environment, whatever package you install now will only be scoped to this environment. So you can <code>pip install scikit-learn-0.17</code> you will only point to <em>THAT</em> package. All other packages that are not in this virtual environment are ignored unless you explicitly add them via methods like <code>pip</code>.</p>

<p>There are many other benefits to virtual environment, high reccomend reading more about them <a href="https://docs.python.org/3/library/venv.html" rel="nofollow">here</a>.     </p>

python python-2.7 scikit-learn python-module