Using Third-party libraries#

Education objectives

  • notion of third-party Python libs

  • good/bad practices

  • lock files and reproducibility

  • established and emerging tools

You want to use Python to execute your own Python code that require third party projects. You have many choices to do this!

General recommendations#

We can give general advices:

Not recommended

  • Installing packages in the system Python

  • Installing packages in the base Python of conda installation

Recommended

  • Use virtual environments

  • When possible, use modern workflows with lock files (i.e. tools like PDM, UV, Pixi)

Tip

Lock files are really a game changer, in particular for reproducibility. Use them when possible!

Few installation methods#

Traditional workflow with venv and pip#

python3 -m venv venv-for-something
. venv-for-something/bin/activate
pip install numpy

or in a project with a requirements.txt file:

python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt

Traditional workflow with conda#

conda create -n venv-for-something
conda activate venv-for-something
conda install numpy

UV in a project using UV#

In a project using UV, one can run

uv sync
. .venv/bin/activate

Pixi in a project using UV#

In a project using Pixi, one can run

pixi shell

New environment using UV#

Following the traditional workflow

uv venv .venv -p 3.13
. .venv/bin/activate
uv pip install numpy

Standard UV workflow:

uv init
uv add numpy
. .venv/bin/activate

New environment using Pixi#

pixi init
pixi add numpy
pixi shell

Environments with install-locked-env#

install-locked-env is a tool that install locked environments in a local directory. It uses UV or Pixi so it is very fast. It can be used for example to install locally the main environment of this Python training:

uvx install-locked-env https://foss.heptapod.net/py-edu-fr/py-edu-fr

or to install a good environment to work with the Computational Fluid Dynamics framework Fluidsim:

uvx install-locked-env https://github.com/fluiddyn/fluidsim/tree/branch/default/pixi-envs/env-fluidsim

Todo

Add more explanations on lock files and reproducibility.