Using Python in HPCC with virtualenv
Python applications usually use packages and modules that require specific versions of libraries. This means one installed application may conflict with another application due to using the same library but with different versions. It is difficult to meet the requirements of every application by one global Python installation. To resolve this issue, users can create an isolated virtual environment with a particular version of Python in a self-contained directory of their home or research space. Any package and the dependent libraries installed inside the directory can be available only through the virtual environment. Different applications can then use different virtual environments to avoid any conflict.
Create and use virtual environments
To create python virtual environments, please make sure your preferred version of Python is loaded. It is also a good idea to create a directory of the python version to store different environments and their applications:
1 2 3 4 5 6 7 8 9 10 11 |
|
Currently, two common tools can be used to create Python virtual environments. Please use only one of them:
-
venv is available for Python 3.3 and later by default. The application
pip
andsetuptools
should be ready to use in HPCC system. A virtual environment can be created by running "python3 -m venv <DIR>
", where<DIR>
is the directory of the created environment. Following is an example of the command, and the directorytutorial
is created for the virtual environment.1 2 3
[UserName@dev-intel18 Python3.6.4]$ python3 -m venv tutorial [UserName@dev-intel18 Python3.6.4]$ ls tutorial bin include lib lib64 pyvenv.cfg
-
virtualenv supports all Python versions. By default, HPCC system has
pip
,setuptools
andwheel
installed and available. Similarly to venv, a virtual environment can be created by executingvirtualenv <DIR>
, where applications of the virtual environment are installed intutorial
directory.1 2 3 4 5 6
[UserName@dev-intel18 Python3.6.4]$ virtualenv tutorial Using base prefix '/opt/software/Python/3.6.4-foss-2018a' New python executable in /mnt/home/UserName/Python3.6.4/tutorial/bin/python Installing setuptools, pip, wheel...done. [UserName@dev-intel18 Python3.6.4]$ ls tutorial bin include lib lib64 pip-selfcheck.json
Please use one of them to create the virtual environment.
The created tutorial
environment can now be used by sourcing the
script file activate
under the bin
directory:
1 2 |
|
where the name inside the parentheses (tutorial)
in front of the
prompt line shows the current Python environment. To leave the
environment, just run "deactivate":
1 2 |
|
and the parentheses disappear. Any time you want to use the tutorial
environment. Simply source the file again:
source ~/Python3.6.4/tutorial/bin/activate
and the environment is
back. More information can be found about venv
or virtualenv.
Install packages from PyPI using pip
The most common usage of pip is to install python packages from the Python Package Index with a requirement specifier. Users can also check other usages with pip. Below, some of the common usage scenarios are introduced.
To install the latest version of a python package, users can run
pip install <Package Name>
, for example, using sympy
as
<Package Name>
:
1 2 3 4 5 6 |
|
To install a specific version of a python package, please run
pip install <Package Name>==<Version Number>
. For example, install
numpy
with <version number>
as 1.16.2
:
1 2 3 4 5 6 |
|
To upgrade an already installed package to the latest from PyPI,
users can run pip install --upgrade
. For example, upgrade the
installed package numpy
:
1 2 3 4 5 6 7 8 |
|
With pip, you can also list all installed packages and their
versions with the command pip freeze
:
1 2 3 4 |
|
For more detail, see the pip docs, which includes a complete Reference Guide.
PYTHONPATH environment variable
You can use the environment variable PYTHONPATH
to include the
packages already installed by the same python version in other
directories. By adding the site-packages
paths to PYTHONPATH
environment variable and separating them with the ":
" symbol:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
all packages inside the paths are now ready to use. Please make sure the
site-packages
path of the current environment is set the first
in PYTHONPATH
variable. If a package is installed in more than one
path (possibly with different versions), the package of the first path
showing in the variable (PYTHONPATH
) will be used.