Installing your own Python Modules

From SciNet Users Documentation
(Redirected from PythonVirtualEnv)
Jump to navigation Jump to search

There are many optional and conflicting packages for Python that users 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, the most common of which are the pip and conda commands. By default, these commands would install in the same directory as the one in which the python executable lives, but python provides a number of ways for users to install libraries in their home directories instead.

One way to do this with pip using the --user option, but you shouldn't. That approach is now mostly superseded by virtual environments, and we do not recommend using the --user option as it can interfere with other Python 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.

Note that the use of conda is highly discouraged on Niagara.

Using Virtualenv in Regular Python

Creation

In the terminal, first load a python module, e.g.

   module load NiaEnv/2019b python/3.11.5

or (on e.g. Teach)

   module load TeachEnv/2022a python/3.11.5

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

   mkdir ~/.virtualenvs

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

   virtualenv --system-site-packages ~/.virtualenvs/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 before activating the environment, while in the NiaEnv/2018a stack, you need to load the python module before activating the environment.

Usage of your virtual environment by others

Sharing a virtual environment with another user is easy. As long as the directory containing the virtual environment is readable by that other user (which on Niagara is the default when that user is in the same group as the directory), then they simply have to source the activate file in the bin directory of that environment, e.g.

   source /home/g/group/user/.virtualenvs/myenv/bin/activate

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 command

   venv2jup

which is nearly equivalent to 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. The advantage of the venv2jup command is that in addition to these two commands, it also corrects some paths in case modules are loaded and checks if all is setup properly. This procedure works for NiaEnv/2020a and NiaEnv/2019b, but may fail for NiaEnv/2018a.

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

Using Virtual Environments in Intelpython/Anaconda

Caveat: Although using conda is possible on Niagara, it is strongly recommended not to do so, as it causes several difficulties.

Creation

One can use the same kind of virtual environments for the intelpython and conda modules as for regular modules. However, environments 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 that conda environments are much more flexible, but also that they do not cooperate well with other software modules on Niagara, and will created 10-100 thousands files that can easily cause issues with your file quota on $HOME. Therefore, you should always use regular virtual environments and pip on Niagara and not conda, unless you have a good reason not too.

First, you just need to load a 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/envs/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/envs, the jupyter notebook should pick them up automatically.

Cleaning up conda

Once the installation of a package finishes, please clean the cache:

conda clean -y --all
rm -rf $HOME/.conda/pkgs/*

If you do no need a conda environment anymore, make sure to remove it:

conda remove --name myPythonEnv --all

To verify that the environment was removed, run:

conda info --envs

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.