Python¶
On the CIRRELT/GERAD computers, there's a version of python installed but it won't be a recent version which could lead to compatibility with some python packages that you want to use.
Modules¶
We have other versions available as modules. You can get a list of the different versions with this command:
you can load the default version like this:
or specify the version you prefer.
Virtual environments¶¶
A python virtual environmenent is a space where you can install packages that don't interfere with packages from another environment. This allows to have multiple projects with different dependencies and not be in a situtation where one needs a specific version and another needs another.
Creating an virtual environment is easy. Go to the directory of your project and use this command:
This will create the project1 virtual environment in the directory where you are. Then, to use the environment and be able to install packages in it, you have to activate it:
When you no longer need that environment or you want to switch, you can use the command:
UV¶
uvis a python manager as well as a python package manager as well as other
tools all in one. It's an alternative to tools like pip, pyenv, venv, pipx,
poetry, etc.
To use it, you only have to load the module:
Installting python¶
uv can be used to install a python interpreter. To obtain the list,
you can use this command:
To do the actual installation, you can do this command:
Note that starting with version 3.13, there's distinct variants of python.
The standard one can be installed by using the number of the version,
the freethreaded on can be installed by adding t after the version number.
For example, 3.13 is normal, 3.13t is freethreaded.
If you're writing a single threaded program, use the standard version, if you write a multi-threaded program, you can try the freethreaded version. Usually, running a single threaded program with the freethreaded version will be slower.
You can install more than one version of python.
Initialization¶
To use uv in a project, you have to initialize it. For exemple in the
project1 directory you can do:
This command will create multiple files:
pyproject.toml: configuration file..python-version: version of python to use.README.md: empty markdown file.main.py: test program.
Installing packages¶
In most projects, you'll use some packages, you can add them like this:
If later you want to remove a package, it's easy. For example, ruff is a tool that's not necessary for the project altough useful. So we remove it.
We could install it as a dev tool instead:
All dependencies are added to the pyproject.toml file that looks like this:
[project]
name = "projet2"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"numpy>=2.4.4",
"polars>=1.40.1",
"ruff>=0.15.11",
"scipy>=1.17.1",
]
[dependency-groups]
dev = [
"ruff>=0.15.11",
]
It's possible to edit that file with a text editor to adjust the dependencies. You should edit that file anyway to adjust the boilerplate text that's put by default.
There's also a uv.lock file in the directory. The difference between the 2 files
is that pyproject.toml contains the list of what you asked and uv.lock
contains the list of what was really installed.
Tools¶
The ruff tool will probably be used in more than 1 project so instead of
installing it as a dev tool, we'll install it as a tool that we can use
with uvx
jupiter is another tool you might be interrested in. We would probably use
this command to install it:
and this will install many packages but at the end you'll get this message:
No executables are provided by package `jupyter`; removing tool
hint: An executable with the name `jupyter` is available via dependency `jupyter-core`.
Did you mean `uv tool install jupyter-core`?
In this case, you should follow the suggestion and install jupyter-core.
We can see the list of the tools we have installed with this command:
As you know, new versions come out all the time so you can update your tools either for all or a specific tool.
Running you program¶
Now that we have our dependencies installed and that our code is ready, we can do this:
This command will activate the virtual environment, check that all the dependencies
from pyproject.toml are up to date and then run the program.
Use the python interpreter¶
Usually, to run the interactive python, you'd simply type python. if you do that
you'll use the system python and not the one you chose for your project so
you won't have any of the packages you installed.
Instead, you should do this:
Synchronise the environment¶
If you are cloning an existing project or inheriting existing code, you can do this:
This command reads uv.lock and installs exactly the same package versions.
This will help with reproducibility.
By default, this command will install everything, including developer tools if any. If you only want to run the code, you can add the option not to install these tools.
Updating Dependencies¶
From time to time, it is necessary to update our dependencies.
For a specific library:
To update everything:
Changing Python Version¶
If your project was initially created with version 3.13 and version 3.14 is now available, you can easily change versions.
First, install the new version as described earlier:
Then, change the default version:
Synchronize for the new version:
uv will recreate the environment with Python 3.14 and reinstall all compatible
dependencies.
If a library is not compatible with the new version, uv lock will warn you.
You will then need to:
- Wait for an update to the library
- Find an alternative
- Restrict the version in
pyproject.toml - Revert to the previous version of Python while waiting
Finally, when everything is correct,
change your pyproject.toml file to indicate that the project requires
the new version. Simply change this in the file:
and remove the Python version you no longer need. Obviously, if you have more than one project, do not remove it until all projects have been migrated to the latest version.
Clearing the cache¶
Once you have finished installing all the libraries you need for your project, you should clear the downloaded files from the cache to free up space in your account.