Pip and Setuptools (setup.py) are the most popular tools for installing Python packages, and also the easiest ways to benefit from the Python performance libraries that come preinstalled on Pawsey systems.
Before you begin
Basic information on these installation tools can be found in their websites (external links):
pip and setuptools are available on Pawsey systems as modules (note the py-
prefix for Python packages):
$ module load python/3.9.7 py-pip/21.1.2
$ module load python/3.9.7 py-setuptools/57.4.0
Pawsey systems also preconfigure the shell environment to provide a meaningful location for user-specific package installations:
export PYTHONUSERBASE=/software/projects/<project-id>/<user-name>/python
export PATH=$PATH:$PYTHONUSERBASE/bin
Install a package using pip
Let's assume you've found a pip-installable package, for instance by browsing the Python Package Index (external link). In this example, we're installing astropy
, a popular package for astronomy.
First, we need to load the relevant system modules, including python
, py-pip
and any required dependency packages (in this case, py-numpy
):
$ module load python/3.9.7 $ module load py-pip/21.1.2-py3.9.7 $ module load py-numpy/1.20.3
Now let's proceed with the installation. We're going to use pip with the --user
flag to install in the Python user directory. (Users cannot install software in system directories on shared Pawsey supercomputer systems.)
$ pip install --user astropy==4.1 Collecting astropy==4.1 Downloading https://files.pythonhosted.org/packages/74/9c/a1e51955d4a2af497a507c323409ebe55c122a91c438d2884d918360efc1/astropy-4.1-cp36-cp36m-manylinux1_x86_64.whl (10.3MB) |████████████████████████████████| 10.3MB 14.7MB/s Requirement already satisfied: numpy>=1.16 in /pawsey/sles12sp3/python/3.6.3/numpy/1.19.0/lib/python3.6/site-packages/numpy-1.19.0-py3.6-linux-x86_64.egg (from astropy==4.1) (1.19.0) Installing collected packages: astropy Successfully installed astropy-4.1
Reproducible installations with pip
Let's go through a simple way of making the installation above more reproducible.
After the installation, we can use pip freeze
to save the list of installed packages and their versions:
$ pip freeze >requirements.txt $ cat requirements.txt astropy==4.1 numpy==1.19.0
If we need to reinstall exactly the same Python environment later on, we can make use of the list we have just created:
$ module load python/3.9.7 $ module load py-pip/21.1.2-py3.9.7 $ module load py-numpy/1.20.3 $ pip install --user --no-deps -r requirements.txt Collecting astropy==4.1 (from -r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/74/9c/a1e51955d4a2af497a507c323409ebe55c122a91c438d2884d918360efc1/astropy-4.1-cp36-cp36m-manylinux1_x86_64.whl (10.3MB) |████████████████████████████████| 10.3MB 16.0MB/s Requirement already satisfied: numpy==1.19.0 in /pawsey/sles12sp3/python/3.6.3/numpy/1.19.0/lib/python3.6/site-packages/numpy-1.19.0-py3.6-linux-x86_64.egg (from -r requirements.txt (line 2)) (1.19.0) Installing collected packages: astropy Successfully installed astropy-4.1
Note how we use the flag --no-deps
to make sure that pip only installs the packages that are listed in the requirements. This is a fully functional list of packages, as we got it from a previous installation.
Installing a package with setuptools
Sometimes you need to install a package without the support of pip, for example, installing the development branch to obtain a bug fix that has not been published yet. In this case, you can still rely on a software-aided procedure.
For example, to install the development branch of the Python package tqdm
, first download the source code:
$ git clone https://github.com/tqdm/tqdm
Change to the tqdm
source code directory and then load the required modules, namely python
and py-setuptools
:
$ module load python/3.9.7 py-setuptools/57.4.0-py3.9.7
Finally, execute the build and installation process:
$ python setup.py build running build running build_py creating build [...] $ python setup.py install --user running install running bdist_egg running egg_info [...] Finished processing dependencies for tqdm==4.61.1
Using virtual environments
Python packages can also be installed in a virtual environment, where all packages will be stored in the virtual environment directory. This virtual environment is a local copy of the installation of Python that can be modified without affecting the original one, which can be a very useful feature. This approach is similar to installing packages locally with --usr
or by using the Conda package manager.
$ python -m venv $MYGROUP/python-venv # create a virtual environment $ source $MYGROUP/python-venv/bin/activate # activate the environment, updating python paths $ pip install astropy==4.1 # install astropy in the environment $ deactivate # deactivate the environment, now cannot load packages installed in venv.
Related pages
- To use containers for packaging and deploying Python workflows, see the Containers page.
- To use the Conda package manager, see the page Conda and Reproducible Installations.