Development/Python: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
(Added more information)
Line 1: Line 1:
= Introduction =
Python is a versatile, easy-to-learn, usually interpreted programming language. It offers a wide range of libraries for scientific tasks and visualization. Python is the de facto standard interface for applications of machine learning. Python can be used in particular as an open source alternative for tasks that have usually been used for Matlab.


== Introduction ==
= Installation and Versions =
Python is a versatile, easy-to-learn, interpreted programming language. It offers a wide range of libraries for scientific tasks and visualization. Python counts to the best languages for machine learning. Python can be used in particular as an open source alternative for tasks that have usually been used for Matlab.
Python is available on all systems. Either as system Python, which comes bundled with the operating system, or via Lmod software modules. Installation is not required.
The available Python versions may differ from site to site.


== System Python ==
== Installation and Versions ==
Python is available on all systems. With <code>python --version</code> you can see the currently active default python version. In general, you can choose from various types of python installations:
System Python is available in different versions, typically the default system Python is too old for most users and applications. Newer versions are therefore installed alongside the standard version. You can access a specific Python version by specifying the version in the Python command.
* '''System python:''' This python version comes together with the operating system and is available upon login to the cluster. Other python versions might be installed along with it. All versions can be seen with
*: <code>ls /usr/bin/python[0-9].*[0-9] | sort -V | cut -d"/" -f4 | xargs</code>
*: They can change over time. You can access a specific Python version by specifying the version in the Python command.
* '''[[Environment_Modules | Software module]]:''' Available versions can be identified via
*: <syntaxhighlight lang="bash">
module avail devel/python
</syntaxhighlight>
* '''Python distributions and virtual environments:''' By using python distributions such as Anaconda, you can easily install the needed python version into a virtual environment. For the use of conda on bwHPC clusters, please refer to [[Development/Conda|Conda]]. Alternatively, you can use more python specific tools for installing python. Some options are listed in [[#Virtual Environments and Package Management | Virtual Environments and Package Management]]
* '''[[Development/Containers | Container]]:''' Containers can contain their own python installation. Keep this in mind when you are working with containers provided by others.


== Running Python Code ==
On bwUniCluster, different versions are currently available, <code>ls /usr/bin/python[0-9].*[0-9] | sort -V | cut -d"/" -f4 | xargs</code> results in: <code>python2.7 python3.6 python3.8 python3.9 python3.11</code>.


There are three ways to run Python commands:
Please note, that these exact versions will change over time and differ from site to site!


* Within a '''terminal''' by executing the comand <code>python</code>. This starts a Python shell, where all commands are evaluated by the Python interpreter.
<syntaxhighlight lang="bash">
* Within a '''script''' (file ends with ''.py'' and can be run with <code>python myProgram.py</code>)
$ python --version # This is system default Python
* Within a '''notebook''' (file ends with .ipynb). You can use other programming languages and markdown within a notebook next to your python code. Besides software development itself, teaching, prototyping and visualization are good use cases for notebooks as well.
Python 3.6.8
$ python3.11 --version
Python 3.11.2
</syntaxhighlight>


== Python Modules ==
== Development Environments ==
Python is also offered via software modules. Available versions can be identified via:
<syntaxhighlight lang="bash">
$ module avail devel/python
</syntaxhighlight>


Development Environments are usually more comfortable than running code directly from the shell. Some common options are:
A specific version of Python can then be chosen e.g. as follows:
<syntaxhighlight lang="bash">
$ module load devel/python/3.12.3_gnu_13.3
$ python --version
Python 3.12.3
</syntaxhighlight>


* [[Jupyter]]
== Python Distributions ==
* [[Development/VS_Code | VS Code]]
If Python versions are required that are not provided by modules or are not installed natively, it is possible to use distributions such as Anaconda. There are use cases where the use of conda is beneficial or, depending on the scientific community, a standard approach to distributing Python packages. For the use of conda on bwHPC clusters, please refer to [[Development/Conda|Conda]].
* PyCharm


== Virtual Environments and Package Management ==
= Usage =


Virtual environments allow Python packages to be installed in a separate installation directory. There should be at least one environment per project. This prevents version conflicts, promotes clarity as to which packages are required for which software and prevents the home directory from being cluttered with libraries that are not (or no longer) required.
There are two ways to run Python commands:
A package can be installed via a package manager and contains an additional set of functions.


=== Overview ===
# Interactive Mode
The following table provides an overview of both and explains the differences between the various options. After deciding on a specific tool, it can be installed by following the given link.
# Script Mode
To make it short, if you plan to use...
* ...Python only: venv is the base for later solutions, poetry is widely used, uv is the latest and fastest option.
* ...Python + Conda: Installing conda packages first into a conda environment and then python packages with pip should work. For a faster and more up to date solution, choose pixi.


{| class="wikitable"
In order to start the interactive mode, simply run the <code>python</code> command.
|- style="text-align:center;"
This starts a Python shell, where all commands are evaluated by the Python interpreter.
! Tool
! Description
! Can install python versions
! Installs packages from PyPI
! Installs packages from conda
! Dependency Resolver
! Dependency Management
! Creates Virtual Environments
! Supports building, packaging and publishing code (to PyPI)
|-
| pyenv
| Manages python versions on your system.
| yes
| no
| no
| no
| no
| no
| no
|-
| pip
| For installing python packages.
| no
| yes
| no
| yes
| no
| no
| no
|-
| venv
| For installing and managing python packages. Part of Python's standard library.
| no
| no
| no
| yes
| yes
| yes
| no
|-
| setuptools
| For packaging python projects.
| no
| no
| no
| no
| no
| no
| yes
|-
| poetry
| For installing and managing python packages. Install it with pipx.
| no
| yes
| no
| yes
| yes
| yes
| yes
|-
| pipx
| For installing and running python applications (like poetry) globally while having them in isolated environments. It is useful for keeping applications globally available and at the same time separated in their own virtual environments. Use it when the installation instructions of an application offer you this way of installation.
| no
| no
| no
| yes
| yes
| yes (only for single applications)
| yes
|-
| uv
| Replaces pixi, poetry, pyenv, pip etc. dand is very fast (https://www.loopwerk.io/articles/2025/uv-keeps-getting-better/)
| yes
| yes
| no
| yes
| yes
| yes
| yes
|-
| pixi
| For installing and managing python as well as conda packages. Uses uv.
| yes
| yes
| yes
| yes
| yes
| yes
| yes
|}


Script mode basically means, that all Python commands are stored in a text file with the extension <b>.py</b>.<br/>
The program will be executed via
<code lang="bash">
python myProgram.py
</code>


=== Pip ===
For teaching purposes, code prototyping and visualization, the use of Jupyter notebooks is advantageous. Further information about interactive supercomputing and the use of Jupyter can be found at [[Jupyter]].


The standard package manager under Python is <code>pip</code>. It can be used to install, update and delete packages. Pip can be called directly or via <code>python -m pip</code>. The standard repository from which packages are obtained is PyPI (https://pypi.org/). When a package depends on others, they are automatically installed as well.
= Package Manager (pip)=
In the following, the most common pip commands are shown exemplarily. Packages should always be installed within virtual environments to avoid conflicting installations. If you decide to not use a virtual environment, the install commands have to be accomplished by a <code>--user</code> flag or controlled via environment variables.

The functionality of Python is extended via modules. The standard package manager under Python is <code>pip</code>. It can be used to install, update and delete packages. <code>pip</code> can be called directly or via <code>python -m pip</code>. The standard repository from which packages are obtained is PyPI (https://pypi.org/). Package dependencies are automatically installed.

== Usage ==
In the following, the most common pip commands are shown exemplarily. Please note, that you should always <b>use pip in conjunction with venv</b>. If you decide to not use a virtual environment, the install commands have to be accomplished by a <code>--user</code> flag or controlled via environment variables.


<b>Installation of packages</b><br/>
<b>Installation of packages</b><br/>
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
$ pip install pandas # Installs the latest compatible version of pandas and its required dependencies
pip install pandas # Installs the latest compatible version of pandas and its required dependencies
$ pip install pandas=1.5.3 # Installs exact version
pip install pandas=1.5.3 # Installs exact version
$ pip install pandas>=1.5.3 # Installs version newer or equal to 1.5.3
pip install pandas>=1.5.3 # Installs version newer or equal to 1.5.3
</syntaxhighlight>
</syntaxhighlight>


The packages from PyPI usually consist of precompiled libraries. However, <code>pip</code> is also capable of creating packages from source code. However, this may require the C/C++ compiler and other dependencies needed to build the libraries. In the example, pip obtains the source code of matplotlib from github.com, installs its dependencies, compiles the library and installs it:
The packages from PyPI usually consist of precompiled libraries. However, <code>pip</code> is also capable of creating packages from source code. However, this may require the C/C++ compiler and other dependencies needed to build the libraries. In the example, pip obtains the source code of matplotlib from github.com, installs its dependencies, compiles the library and installs it:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
$ pip install git+https://github.com/matplotlib/matplotlib
pip install git+https://github.com/matplotlib/matplotlib
</syntaxhighlight>
</syntaxhighlight>
<b>Upgrade packages</b><br/>

Several external packages are usually required for a software project. It is advisable to create the file <code>requirements.txt</code>, i.e. a text file with all dependencies:

<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
$ pip install -r requirements.txt # Installs these packages in one go
pip install --upgrade pandas # Updates the library if update is available
</syntaxhighlight>
</syntaxhighlight>

<b>Removing packages</b><br/>
<b>Removing packages</b><br/>
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
$ pip uninstall pandas # Removes pandas
pip uninstall pandas # Removes pandas
</syntaxhighlight>

<b>Upgrade packages</b><br/>
<syntaxhighlight lang="python">
$ pip install --upgrade pandas # Updates the library if update is available
</syntaxhighlight>
</syntaxhighlight>
<b>Show packages</b><br/>
<b>Show packages</b><br/>
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
$ pip list # Shows the installed packages
pip list # Shows the installed packages
pip freeze # Shows the installed packages and their versions
</syntaxhighlight>
</syntaxhighlight>
<b>Save State</b><br/>
To allow for reproducibility it is important to provide information about the full list of packages and their exact versions [https://pip.pypa.io/en/stable/topics/repeatable-installs/ see version pinning].
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
$ pip freeze # Shows the installed packages and their versions
pip freeze > requirements.txt # redirect package and version information to a textfile
pip install -r requirements.txt # Installs all packages that are listed in the file
</syntaxhighlight>
</syntaxhighlight>
The output can be redirected to a <code>requirements.txt</code> file: <code>$ pip freeze > requirements.txt</code><br/>
This is recommended if version pinning (https://pip.pypa.io/en/stable/topics/repeatable-installs/) is explicitly desired.


=== Venv ===
= Virtual Environments (venv) =
Virtual environments allow Python packages to be installed separately in a separate installation directory for a specific application instead of installing them globally. This prevents version conflicts, promotes clarity as to which packages are required for which software and prevents the home directory from being cluttered with libraries that are not (or no longer) required.


<b>It is highly recommended to always use venv whenever a package is to be installed.</b>
== Creation ==
The module <code>venv</code> enables the creation of a virtual environment and is a standard component of Python. Creating a <code>venv</code> means that a folder is created which contains a separate copy of the Python binary file as well as <code>pip</code> and <code>setuptools</code>. After activating the <code>venv</code>, the binary file in this folder is used when <code>python</code> or <code>pip</code> is called. This folder is also the installation target for other Python packages.
The module <code>venv</code> enables the creation of a virtual environment and is a standard component of Python. Creating a <code>venv</code> means that a folder is created which contains a separate copy of the Python binary file as well as <code>pip</code> and <code>setuptools</code>. After activating the <code>venv</code>, the binary file in this folder is used when <code>python</code> or <code>pip</code> is called. This folder is also the installation target for other Python packages.

==== Creation ====


Create, activate, install software, deactivate:
Create, activate, install software, deactivate:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ python3.11 -m venv myEnv # Create venv
python3.11 -m venv myEnv # Create venv
$ source myEnv/bin/activate # Activate venv
source myEnv/bin/activate # Activate venv
$ pip install --upgrade pip # Update of the venv-local pip
pip install --upgrade pip # Update of the venv-local pip
$ pip install <list of packages> # Install packages/modules
pip install <list of packages> # Install packages/modules
$ deactivate
deactivate
</syntaxhighlight>
</syntaxhighlight>


Install list of software:
Install list of software:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ pip install -r requirements.txt # Install packages/modules
pip install -r requirements.txt # Install packages/modules
</syntaxhighlight>
</syntaxhighlight>


== Usage ==
==== Usage ====
To use the virtual environment after all dependencies have been installed there, it is sufficient to simply activate it:
To use the virtual environment after all dependencies have been installed there, it is sufficient to simply activate it:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ source myEnv/bin/activate # Activate venv
source myEnv/bin/activate # Activate venv
</syntaxhighlight>
</syntaxhighlight>


Line 136: Line 209:
</syntaxhighlight>
</syntaxhighlight>


=== Poetry ===
= Best Practice =

* Always use virtual environments! One venv per project.
==== Creation ====
When you want to create a virtual environment for an already existing project, you can go to the top directory and run
<syntaxhighlight lang="bash">
poery init # Create virtual enviroment
</syntaxhighlight>
Otherwise start with the demo project:
<syntaxhighlight lang="bash">
poetry new poetry-demo
</syntaxhighlight>
You can set the allowed python versions in the pyproject.toml. To switch between python installations on your sytem, you can use
<syntaxhighlight lang="bash">
poetry env use python3.11
</syntaxhighlight>

==== Usage ====

Install and update packages
<syntaxhighlight lang="bash">
poetry install <package_name>
poetry update # Update to latest versions of packages
</syntaxhighlight>

To execute something within the virtual environment:
<syntaxhighlight lang="bash">
poetry run <command>
</syntaxhighlight>

Helpful links:
* [https://python-poetry.org/docs/managing-environments/#activating-the-environment Activate Environment]

Helpful commands
<syntaxhighlight lang="bash">
poetry env info # show environment information
poetry env list # list all virtual environments associated with the current project
poetry env list --full-path
poetry env remove # delete virtual environment
</syntaxhighlight>

== Best Practices ==
* Always use virtual environments! Use one environment per project.
* If a new or existing Python project is to be created or used, the following procedure is recommended:
* If a new or existing Python project is to be created or used, the following procedure is recommended:
# Version the Python source files and the <code>requirements.txt</code> file with a version control system, e.g. git. When versioning, exclude the <code>venv</code> folder via an entry in the <code>.gitignore</code> file.
# Version the Python source files and the <code>requirements.txt</code> file with a version control system, e.g. git. Exclude unnecessary folders and files like for example <code>venv</code> via an entry in the ignore file, for example <code>.gitignore</code>.
# Create and activate a <code>venv</code>.
# Create and activate a virtual environment.
# Update the venv-local <code>pip</code>.
# Update pip <code>pip install --upgrade pip</code>.
# Install the all required packages via a <code>requirements.txt</code> file.
# Install all required packages via the <code>requirements.txt</code> file in the case of venv. Or by using the corresponding command of your chosen tool.
* List or dictionary comprehensions are to be prefered over loops as they are in general faster.
* Be aware of the differences between references, shallow and deep copies.
* Do not parallelize by hand but use libraries were possible (Dask, ...).





Revision as of 10:04, 9 September 2025

Introduction

Python is a versatile, easy-to-learn, interpreted programming language. It offers a wide range of libraries for scientific tasks and visualization. Python counts to the best languages for machine learning. Python can be used in particular as an open source alternative for tasks that have usually been used for Matlab.

Installation and Versions

Python is available on all systems. With python --version you can see the currently active default python version. In general, you can choose from various types of python installations:

  • System python: This python version comes together with the operating system and is available upon login to the cluster. Other python versions might be installed along with it. All versions can be seen with
    ls /usr/bin/python[0-9].*[0-9] | sort -V | cut -d"/" -f4 | xargs
    They can change over time. You can access a specific Python version by specifying the version in the Python command.
  • Software module: Available versions can be identified via
      module avail devel/python
    
  • Python distributions and virtual environments: By using python distributions such as Anaconda, you can easily install the needed python version into a virtual environment. For the use of conda on bwHPC clusters, please refer to Conda. Alternatively, you can use more python specific tools for installing python. Some options are listed in Virtual Environments and Package Management
  • Container: Containers can contain their own python installation. Keep this in mind when you are working with containers provided by others.

Running Python Code

There are three ways to run Python commands:

  • Within a terminal by executing the comand python. This starts a Python shell, where all commands are evaluated by the Python interpreter.
  • Within a script (file ends with .py and can be run with python myProgram.py)
  • Within a notebook (file ends with .ipynb). You can use other programming languages and markdown within a notebook next to your python code. Besides software development itself, teaching, prototyping and visualization are good use cases for notebooks as well.

Development Environments

Development Environments are usually more comfortable than running code directly from the shell. Some common options are:

Virtual Environments and Package Management

Virtual environments allow Python packages to be installed in a separate installation directory. There should be at least one environment per project. This prevents version conflicts, promotes clarity as to which packages are required for which software and prevents the home directory from being cluttered with libraries that are not (or no longer) required. A package can be installed via a package manager and contains an additional set of functions.

Overview

The following table provides an overview of both and explains the differences between the various options. After deciding on a specific tool, it can be installed by following the given link. To make it short, if you plan to use...

  • ...Python only: venv is the base for later solutions, poetry is widely used, uv is the latest and fastest option.
  • ...Python + Conda: Installing conda packages first into a conda environment and then python packages with pip should work. For a faster and more up to date solution, choose pixi.
Tool Description Can install python versions Installs packages from PyPI Installs packages from conda Dependency Resolver Dependency Management Creates Virtual Environments Supports building, packaging and publishing code (to PyPI)
pyenv Manages python versions on your system. yes no no no no no no
pip For installing python packages. no yes no yes no no no
venv For installing and managing python packages. Part of Python's standard library. no no no yes yes yes no
setuptools For packaging python projects. no no no no no no yes
poetry For installing and managing python packages. Install it with pipx. no yes no yes yes yes yes
pipx For installing and running python applications (like poetry) globally while having them in isolated environments. It is useful for keeping applications globally available and at the same time separated in their own virtual environments. Use it when the installation instructions of an application offer you this way of installation. no no no yes yes yes (only for single applications) yes
uv Replaces pixi, poetry, pyenv, pip etc. dand is very fast (https://www.loopwerk.io/articles/2025/uv-keeps-getting-better/) yes yes no yes yes yes yes
pixi For installing and managing python as well as conda packages. Uses uv. yes yes yes yes yes yes yes


Pip

The standard package manager under Python is pip. It can be used to install, update and delete packages. Pip can be called directly or via python -m pip. The standard repository from which packages are obtained is PyPI (https://pypi.org/). When a package depends on others, they are automatically installed as well. In the following, the most common pip commands are shown exemplarily. Packages should always be installed within virtual environments to avoid conflicting installations. If you decide to not use a virtual environment, the install commands have to be accomplished by a --user flag or controlled via environment variables.

Installation of packages

pip install pandas           # Installs the latest compatible version of pandas and its required dependencies
pip install pandas=1.5.3     # Installs exact version
pip install pandas>=1.5.3    # Installs version newer or equal to 1.5.3

The packages from PyPI usually consist of precompiled libraries. However, pip is also capable of creating packages from source code. However, this may require the C/C++ compiler and other dependencies needed to build the libraries. In the example, pip obtains the source code of matplotlib from github.com, installs its dependencies, compiles the library and installs it:

pip install git+https://github.com/matplotlib/matplotlib

Upgrade packages

pip install --upgrade pandas # Updates the library if update is available

Removing packages

pip uninstall pandas         # Removes pandas

Show packages

pip list           # Shows the installed packages
pip freeze         # Shows the installed packages and their versions

Save State
To allow for reproducibility it is important to provide information about the full list of packages and their exact versions see version pinning.

pip freeze > requirements.txt     # redirect package and version information to a textfile
pip install -r requirements.txt   # Installs all packages that are listed in the file

Venv

The module venv enables the creation of a virtual environment and is a standard component of Python. Creating a venv means that a folder is created which contains a separate copy of the Python binary file as well as pip and setuptools. After activating the venv, the binary file in this folder is used when python or pip is called. This folder is also the installation target for other Python packages.

Creation

Create, activate, install software, deactivate:

python3.11 -m venv myEnv        # Create venv
source myEnv/bin/activate       # Activate venv
pip install --upgrade pip       # Update of the venv-local pip
pip install <list of packages>  # Install packages/modules
deactivate

Install list of software:

pip install -r requirements.txt # Install packages/modules

Usage

To use the virtual environment after all dependencies have been installed there, it is sufficient to simply activate it:

source myEnv/bin/activate       # Activate venv

With venv activated the terminal prompt will reflect that accordingly:

(myEnv) $

It is no longer necessary to specify the Python version, the simple command python starts the Python version that was used to create the virtual environment.
You can check, which Python version is in use via:

(myEnv) $ which python
</path/to/project>/myEnv/bin/python

Poetry

Creation

When you want to create a virtual environment for an already existing project, you can go to the top directory and run

poery init      # Create virtual enviroment

Otherwise start with the demo project:

poetry new poetry-demo

You can set the allowed python versions in the pyproject.toml. To switch between python installations on your sytem, you can use

poetry env use python3.11

Usage

Install and update packages

poetry install <package_name>
poetry update      # Update to latest versions of packages

To execute something within the virtual environment:

poetry run <command>

Helpful links:

Helpful commands

poetry env info # show environment information
poetry env list # list all virtual environments associated with the current project 
poetry env list --full-path
poetry env remove # delete virtual environment

Best Practices

  • Always use virtual environments! Use one environment per project.
  • If a new or existing Python project is to be created or used, the following procedure is recommended:
  1. Version the Python source files and the requirements.txt file with a version control system, e.g. git. Exclude unnecessary folders and files like for example venv via an entry in the ignore file, for example .gitignore.
  2. Create and activate a virtual environment.
  3. Update pip pip install --upgrade pip.
  4. Install all required packages via the requirements.txt file in the case of venv. Or by using the corresponding command of your chosen tool.
  • List or dictionary comprehensions are to be prefered over loops as they are in general faster.
  • Be aware of the differences between references, shallow and deep copies.
  • Do not parallelize by hand but use libraries were possible (Dask, ...).