Installing your own Python Modules: Difference between revisions

From SciNet Users Documentation
Jump to navigation Jump to search
m Rzon moved page PythonVirtualEnv to Installing your own Python Modules: Page is unlikely to be found by people that do nor know that they need a python virtual env
No edit summary
Line 1: Line 1:
=Python Virtual Environments=
=Installing Python Packages=


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 default python environment.
There are many optional and conflicting packages for Python people could potentially want (see e.g. http://pypi.python.org/pypi). Therefore, users need to install these additional packages locally in their home directories. In fact, there is no choice, as users do not have permissions to install packages system-wide.


VirtualEnv can be used either with the default python modules or the anaconda ones.
Python provides a number of ways to install packages, but the most common one these days are the <tt>pip</tt> and <tt>conda</tt> command.  By default, these commands would install in the same directory as the one in which the python executable lives.


== Using Python VirtualEnv in Anaconda ==
Luckily, python provides a number of ways for users to install the libraries they need in their home directories. 
One way to do this with <tt>pip</tt> using the <tt>--user</tt> option, but that approach is now mostly superseded by virtual environments.


VirtualEnv are right built-in in Anaconda, see [https://conda.io/docs/user-guide/tasks/manage-environments.html]
Virtual environments 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 default python environment.
You just need to load the proper anaconda module, eg.


<source lang="bash">
Virtual environments can be used either with the [[Python#Regular_Python | regular python modules]] or the [[Python#Intel_Python | intelpython/anaconda]] modules.
 # load the anaconda module
 module load python/3.6.4-anaconda5.1.0


 # create a virtual env.
= Using Python Virtualenv in Regular Python =
 conda create -n myPythonEnv python=3.6


 # activate your vitual env.
==Creation==
 source activate myPythonEnv
First load a python module:
 
    module load NiaEnv/2019b python/2.6.8
 
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 <code>myEnv</code> choose any name you like:
 
    virtualenv --system-site-packages myEnv
 
The "--system-site-packages" flag will use the system-installed versions of packages rather than installing them anew (the list of these packages can be found on the [[Python]] wiki page).  This will result in fewer files created in your virtual environment.  After that you can activate that virtual environment:
 
    source ~/.virtualenvs/myenv/bin/activate
 
As you are in the virtualenv now, you can just type <code>pip install <required module></code> to install any module into your virtual environment.
 
To go back to the normal python installation simply type
 
    deactivate
 
==Command line and job usage==


 # at this point you are in your own environment and can just do the installation of any package that you need, eg.
You need to activate the appropriate environment every time you log in, and at the start of all your jobs scripts.  However, the installation of packages only needs to be done once.  In the NiaEnv/2019b stack, it is not necessary to load the python module befire activating the environment, while in the NiaEnv/2018a stack, you did need to load the python module before activating the environment.
 pip install myFAVpackage


</source>
==Usage in the Jupyter Hub==


== Using Python Virtualenv in plain Python ==
You can use your virtual environment in Niagara's [[Jupyter_Hub]], but there are two additional steps required to get the JupterHub to know about your environment and to make it as one of its possible "kernels" for new notebooks.


First load a python module:
After having activated your environment, execute the following two commands
 
    pip install ipykernel
    python -m ipykernel install --name NAME --user
 
The first installs the packages needed to interface with jupyter as a kernel, the latter puts an entry in the <tt>.share/jupyter</tt> directory, in which the jupyterhub looks for possible kernels.
 
For conda environments that were installed in .conda/venv, the jupyter notebook should pick them up automatically.
 
= Using Virtual Environments in Intelpython/Anaconda =
 
==Creation==
 
One can use the same kind of virtual environments for the intelpython and conda modules as for regular modules. However,
environment are built-in in Anaconda, see [https://conda.io/docs/user-guide/tasks/manage-environments.html].  These "conda environments" are not the same as regular virtual environments, as they can contain general packages, such as compilers.  The latter feature means conda environments are much more flexible, but also are why they do not work well, generally, with other software modules on Niagara.
 
First, you just need to load the conda-like module, e.g.
 
   module load NiaEnv/2019b intelpython3
 
Then, you create a virtual environment
 
   conda create -n myPythonEnv python=3.6
 
(conda puts the environment in the directory <tt>$HOME/.conda/venv/myPythonEnv</tt>)
 
Next, you activate your conda environment:


<source lang="bash">
   source activate myPythonEnv
module load python/2.7.14
</source>


Then create a directory for the virtual environments.
At this point you are in your own environment and can just do the installation of any package that you need, e.g.
One can put an virtual environment anywhere, but this directory structure is recommended:


<source lang="bash">
    pip install myFAVpackage
mkdir ~/.virtualenvs
cd ~/.virtualenvs
</source>


Now we create our first virtualenv called <code>myEnv</code> choose any name you like:
or
    conda install myFAVpackage


<source lang="bash">
To go back to the normal python installation, type
virtualenv --system-site-packages myEnv
   
</source>
    source deactivate


The "--system-site-packages" flag will use the system-installed versions of packages rather than installing them anew.  This will result in fewer files created in your virtual environment.  After that you can activate that virtual environment:
==Command line and job usage==


<source lang="bash">
You need to load the intelpython/anaconda module and activate the appropriate environment every time you log in, and at the start of all your jobs scripts.  However, the installation of packages only needs to be done once.  
source ~/.virtualenvs/myenv/bin/activate
</source>


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


You can use conda environment in Niagara's [[Jupyter_Hub]]. If they were installed in .conda/venv, the jupyter notebook should pick them up automatically.


===Installing the Scientific Python Suite===
=Installing the Scientific Python Suite=


For many scientific codes the packages ''numpy'', ''scipy'', ''matplotlib'', ''pandas'' and ''ipython'' are used.
For many scientific codes the packages ''numpy'', ''scipy'', ''matplotlib'', ''pandas'' and ''ipython'' are used. Versions of these are already in the python modules (except for the regular python modules in the NiaEnv/2018a stack).
All of these install very simply in a virtualenv using the <code>pip install <package name></code>.
However, if you need different versions, you could start your virtual environment without <tt>--system-site-packages</tt>.  In that case, for regular python modules, please install versions of package with an <tt>intel-</tt> prefix, if they exists, so that you will get the most optimized version of the package.

Revision as of 02:34, 11 May 2019

Installing Python Packages

There are many optional and conflicting packages for Python people could potentially want (see e.g. http://pypi.python.org/pypi). Therefore, users need to install these additional packages locally in their home directories. In fact, there is no choice, as users do not have permissions to install packages system-wide.

Python provides a number of ways to install packages, but the most common one these days are the pip and conda command. By default, these commands would install in the same directory as the one in which the python executable lives.

Luckily, python provides a number of ways for users to install the libraries they need in their home directories. One way to do this with pip using the --user option, but that approach is now mostly superseded by virtual environments.

Virtual environments 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 default python environment.

Virtual environments can be used either with the regular python modules or the intelpython/anaconda modules.

Using Python Virtualenv in Regular Python

Creation

First load a python module:

   module load NiaEnv/2019b python/2.6.8

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 --system-site-packages myEnv

The "--system-site-packages" flag will use the system-installed versions of packages rather than installing them anew (the list of these packages can be found on the Python wiki page). This will result in fewer files created in your virtual environment. After that you can activate that virtual environment:

   source ~/.virtualenvs/myenv/bin/activate 

As you are in the virtualenv now, you can just type pip install <required module> to install any module into your virtual environment.

To go back to the normal python installation simply type

   deactivate

Command line and job usage

You need to activate the appropriate environment every time you log in, and at the start of all your jobs scripts. However, the installation of packages only needs to be done once. In the NiaEnv/2019b stack, it is not necessary to load the python module befire activating the environment, while in the NiaEnv/2018a stack, you did need to load the python module before activating the environment.

Usage in the Jupyter Hub

You can use your virtual environment in Niagara's Jupyter_Hub, but there are two additional steps required to get the JupterHub to know about your environment and to make it as one of its possible "kernels" for new notebooks.

After having activated your environment, execute the following two commands

   pip install ipykernel
   python -m ipykernel install --name NAME --user

The first installs the packages needed to interface with jupyter as a kernel, the latter puts an entry in the .share/jupyter directory, in which the jupyterhub looks for possible kernels.

For conda environments that were installed in .conda/venv, the jupyter notebook should pick them up automatically.

Using Virtual Environments in Intelpython/Anaconda

Creation

One can use the same kind of virtual environments for the intelpython and conda modules as for regular modules. However, environment are built-in in Anaconda, see [1]. These "conda environments" are not the same as regular virtual environments, as they can contain general packages, such as compilers. The latter feature means conda environments are much more flexible, but also are why they do not work well, generally, with other software modules on Niagara.

First, you just need to load the conda-like module, e.g.

   module load NiaEnv/2019b intelpython3

Then, you create a virtual environment

   conda create -n myPythonEnv python=3.6

(conda puts the environment in the directory $HOME/.conda/venv/myPythonEnv)

Next, you activate your conda environment:

   source activate myPythonEnv

At this point you are in your own environment and can just do the installation of any package that you need, e.g.

   pip install myFAVpackage

or

   conda install myFAVpackage

To go back to the normal python installation, type

   source deactivate

Command line and job usage

You need to load the intelpython/anaconda module and activate the appropriate environment every time you log in, and at the start of all your jobs scripts. However, the installation of packages only needs to be done once.

Usage in the Jupyter Hub

You can use conda environment in Niagara's Jupyter_Hub. If they were installed in .conda/venv, the jupyter notebook should pick them up automatically.

Installing the Scientific Python Suite

For many scientific codes the packages numpy, scipy, matplotlib, pandas and ipython are used. Versions of these are already in the python modules (except for the regular python modules in the NiaEnv/2018a stack).

However, if you need different versions, you could start your virtual environment without --system-site-packages. In that case, for regular python modules, please install versions of package with an intel- prefix, if they exists, so that you will get the most optimized version of the package.