Installing Python packages and environments¶
In this tutorial you will learn
- How to install Python packages with pip
- How to containerize a Conda environment with Tykky
This tutorial is done on Roihu, which requires that:
- You have a user account at CSC.
- Your account belongs to a project that has access to the Roihu service.
To run Python applications, first load a suitable Python module. CSC has
several Python environments available with a focus on different application
areas, e.g. data science and machine learning,
as well as geoinformatics.
For more details, please see the Python page
and Python usage guide
in Docs CSC.
By selecting a suitable Python environment to start with, you'll minimize
the need to install additional packages.
Note that Conda environments should be containerized according to our
usage policy.
Use the Tykky container wrapper
to accomplish this easily!
Installing Python packages¶
To install simple packages it is usually enough to use
pip, for example:
Remember to include
--user. By default, pip tries to install to the
system Python installation path, which will not work.
For more complex installations you should create a
containerized environment.
See the Docs CSC pages for each Python module for more details, as there may
be some environment-specific instructions.
Example: Installing a simple package with pip¶
Let's install a library called
coverage.
-
Load a Python module and check whether the library is already installed:
-
The error message shows that the library is not available:
-
Check where
--userinstalls will go. Thepython-datamodule setsPYTHONUSERBASEfor you, to an architecture-specific path:echo $PYTHONUSERBASE # roihu-cpu login: /users/<user>/.local/x86_64 # roihu-gpu login: /users/<user>/.local/aarch64On Roihu the user-install directory is not the plain
$HOME/.localassumed by generic guides. It is split per CPU architecture, because the CPU and GPU login nodes use different processors. A package installed with--useron one node is not visible on the other — install and run on the same login node. -
(Optional) To install into a larger area than
$HOME, setPYTHONUSERBASEbefore installing:Set this before
pip install. The module only setsPYTHONUSERBASEwhen it is unset, so once you export your own value it stays until you open a new shell. If you change it between installing and uninstalling,piplooks in the new location and will not find the package. -
Install the library:
-
Re-test to confirm the import now works:
No error means the import was successful.
-
To locate or uninstall the package later,
pipuses the currentPYTHONUSERBASE. As long as you have not changed it since installing:
Note, if the package you installed also contains executable files, i.e. a
command-line interface, these commands may not work as is! This is because the
Python modules provided by CSC are containerized and the user-installed
binaries will refer to an inaccessible Python path inside the container. For
workaround instructions, see our
Python usage guide,
or install your own environment from the beginning inside a container as
outlined in the following example.
Example: Containerizing a Conda environment with Tykky¶
Let's create a containerized Conda environment using the Tykky wrapper.
-
Create a folder under your project's
/projappldirectory for the installation, e.g.: -
Create an
env.ymlenvironment file defining the packages to be installed. Using for examplenano, copy/paste the following contents to the file: -
Purge your current module environment and load the Tykky module:
-
Create and containerize the Conda environment using the
conda-containerizecommand:conda-containerize new --prefix /projappl/<project>/$USER/tykky-env env.yml # replace <project> with your CSC project, e.g. project_2001234This process can take several minutes so be patient.
-
As instructed by Tykky, add the path to the installation
bindirectory to your$PATH:
Adding this to your
$PATH allows you to call Python and all other
executables installed by Conda as if you had activated a
non-containerized Conda environment.
The above Conda installation would create more than 40k files if installed
directly on the parallel file system. Containerizing the environment with Tykky
decreases this to less than 200, thus avoiding Lustre performance issues.
To modify an existing Tykky-based Conda environment, you can use the
update sub-command of conda-containerize together with the --post-install
option to specify a bash script with commands to run to update the installation.
See more details in Docs CSC.