Difference between revisions of "Installing your own Python Modules"

From SciNet Users Documentation
Jump to: navigation, search
(Using Python Virtualenv in plain Python)
(Using Python Virtualenv in plain Python)
Line 32: Line 32:
 
module load python/2.7.14
 
module load python/2.7.14
 
</source>
 
</source>
 
  
 
Then create a directory for the virtual environments.
 
Then create a directory for the virtual environments.
Line 42: Line 41:
 
</source>
 
</source>
  
Now we create our first virtualenv called ''myenv'' choose any name you like:
+
Now we create our first virtualenv called ''myEnv'' choose any name you like:
  
 
<source lang="bash">
 
<source lang="bash">
Line 54: Line 53:
 
</source>
 
</source>
  
To go back to the normal python installation simply type "deactivate"
+
To go back to the normal python installation simply type ''deactivate''.
As you are in the virtualenv now you can just type ''pip install <required module>'' to install any module in your virtual environment
+
As you are in the virtualenv now you can just type ''pip install <required module>'' to install any module in your virtual environment.
 
 
 
 
<!--
 
===Installing the scientific python suite===
 
 
 
For many scientific codes the packages ''numpy'', ''scipy'', ''matplotlib'', ''pandas'' and ''ipython'' are used. Except ''scipy'' all of these install very simply in a virtualenv using the ''pip install <package name>''.  
 
  
For installing ''scipy'' we first need to install ''numpy'' and then copy scinet's ''site.cfg'' (contains paths to the required libraries for scipy).
 
So before doing "pip install scipy" do
 
  
<source lang="bash">
+
===Installing the Scientific Python Suite===
cp /scinet/gpc/tools/Python/Python275-shared-intel/lib/python2.7/site-packages/numpy/distutils/site.cfg ~/.virtualenvs/myenv/lib/python2.7/site-packages/numpy/distutils/
 
</source>
 
  
and then just do ''pip install scipy''
+
For many scientific codes the packages ''numpy'', ''scipy'', ''matplotlib'', ''pandas'' and ''ipython'' are used.
-->
+
All of these install very simply in a virtualenv using the ''pip install <package name>''.

Revision as of 00:28, 29 May 2018

Python Virtual Environments

Virtual environments (short virtualenv) are a standard in Python to create isolated Python environments. This is useful when certain modules or certain versions of modules are not available in the thee default python environment.

VirtualEnv can be used either with the default python modules or the anaconda ones.

Using Python VirtualEnv in Anaconda

VirtualEnv are right builtin in Anaconda, see [1] You will just need to load the proper anaconda module, eg.

 # load the anaconda module
 module load python/3.6.4-anaconda5.1.0

 # create a virtual env.
 conda create -n myPythonEnv python=3.6

 # activate your vitual env.
 source activate myPythonEnv

 # at this point you are in your own environment and can just do the installation of any package that you need, eg.
 pip install myFAVpackage

Using Python Virtualenv in plain Python

First load a python module:

module load python/2.7.14

Then create a directory for the virtual environments. One can put an virtual environment anywhere, but this directory structure is recommended:

mkdir ~/.virtualenvs
cd ~/.virtualenvs

Now we create our first virtualenv called myEnv choose any name you like:

virtualenv myEnv

After that you can activate that virtual environment:

source ~/.virtualenvs/myenv/bin/activate

To go back to the normal python installation simply type deactivate. As you are in the virtualenv now you can just type pip install <required module> to install any module in your virtual environment.


Installing the Scientific Python Suite

For many scientific codes the packages numpy, scipy, matplotlib, pandas and ipython are used. All of these install very simply in a virtualenv using the pip install <package name>.