Python

We provide some packages that come with the current Debian GNU/Linux stable release, but usually this is because they are dependencies of an installed software. For python we strongly recommend to build own python environments with the desired python versions and packages.

Our recommended way to install such environments is trough conda, especially if you want to build a tool or toolchain where the setup will possibly be published in a paper. Alternatively, building an environment via pyenv is possible.

For just quickly trying out some python tool a local installation of pip is recommended.

Installing your own python environment with Conda

For a detailed overview of conda please follow the Conda documentation.

Installing your own python versions with pyenv

pyenv is a collection of tools that allow users to manage different versions of python. The simplest case is to install python in your user space. Using this custom python installation, you will be able to install additional packages in a comfortable way, since you can install them in the "system path" (which is then somewhere within your home).

The documentation on pyenv can be found on its Github page at github.com/pyenv/pyenv.

Here is a small howto for installing python 3.9.1 in your home:

Documentation of pyenv

Installation of a local pip

pip can be installed in a user's environment and work with the python version installed on the system. Every package will be installed for the user only in one location, there is no separation with virtual environments.

The following commands set up a local pip in a location of your choice. As example /scratch/$USER/local is used. You may use a location of your choice, preferrably outside your $HOME as not to impact your quota.

export PYTHONUSERBASE=/scratch/$USER/local
mkdir -p $PYTHONUSERBASE/bin
export PIP_USER=true
export PATH=$PYTHONUSERBASE/bin:$PATH
wget https://bootstrap.pypa.io/get-pip.py -O ~/.local/bin/get-pip.py
python3 ~/.local/bin/get-pip.py -vvv --user

Set default installations to the user's environment permanently (stored in ~/.config/pip/pip.conf):

pip config set install.user true

The exported environment variables will be lost after closing the shell. To enable local pip on demand, add the following function to your .bashrc:

function localpip {
    PYTHONUSERBASE=/scratch/$USER/local
    PATH=$PYTHONUSERBASE/bin:$PATH
    export PYTHONUSERBASE PATH
}

When you open a new shell, entering the command localpip will call the function and initialize your local pip installation.

pip cache

pip uses a cache which is by default stored under ~/.cache/pip or $XDG_CACHE_HOME/pip if it is set to a non-default location. This cache tends to fill up quickly and should occasionally be cleared with

pip cache purge

It is advisable to set the cache's location to the local scratch disk to avoid using up quota:

  1. Create a directory for the cache:

    mkdir -p /scratch/$USER/pip_cache
    
  2. Temporarily set the environment variable to tell pip to use a different cache location:

    export PIP_CACHE_DIR=/scratch/$USER/pip_cache/
    

    or store the location permanently (in ~/.config/pip/pip.conf):

    pip config set global.cache-dir /scratch/$USER/pip_cache
    
  3. Check if the cache location is correct:

    pip cache info
    

Installation of additional or newer packages with pip

Once you installed your custom python with the explanations given above, you are ready to install additional or newer packages the easy way. The usage of pip is very easy. The following command installs the package numpy.

pip install numpy

while the next command would upgrade an existing installation of numpy

pip install --upgrade numpy

pip package management

Show installed packages:

pip list --user 

Show installed packages with their dependencies:

pip freeze user | cut -d '=' -f 1 | xargs -r pip show | grep -E '^(Name|Required-by):'

Show installed packages without dependencies:

pip list --user --not-required --format freeze --exclude pip --exclude setuptools

Show outdated packages

pip list --user --outdated

Update all outdated packages:

pip list --user --outdated |
    awk '{if ($2 ~ /[0-9\.]+/) print $1}' |
    xargs -r pip install --user --upgrade

For advanced usage of pip, please consult the official user guide.

Installation of Python packages that are not available in the archives of pip

Here we provide some shell script snippets for installing frequently asked packages which cannot be installed through pip. These scripts just provide an example installation. You might have to adapt some paths in order to make the package work correctly with the version of python you are using (e.g. if you run your custom python provided through pyenv).

nlopt

#!/bin/bash

# Installation script for nlopt library

VERSION=2.3
INSTALLDIR=$HOME/.local
BUILDDIR=/scratch/$USER/nlopt

mkdir -p $BUILDDIR
cd $BUILDDIR

wget "http://ab-initio.mit.edu/nlopt/nlopt-${VERSION}.tar.gz"
tar -xvvzkf nlopt-${VERSION}.tar.gz
cd nlopt-${VERSION}

./configure \
        --enable-shared \
        --prefix=$INSTALLDIR \
        OCT_INSTALL_DIR=$INSTALLDIR/octave/oct \
        M_INSTALL_DIR=$INSTALLDIR/octave/m/ \
        MEX_INSTALL_DIR=$INSTALLDIR/mex \
        GUILE_INSTALL_DIR=$INSTALLDIR/guile

make
make install

Debugging python code

To debug python code with Visual Studio Code, Microsoft provides the Debug Adapter Protocol (DAP) and a python implementation debug.py.

For details consult the section Python debugging in VS Code in the official VS Code manual


CategoryLXSW

Programming/Languages/Python (last edited 2023-11-06 08:33:58 by stroth)