type(3) # type is a built-in function
int
Marie-Hélène Burle
So far, we have talked about functionality that is available whenever you launch Python. Python however comes with a lot more capabilities, some of it out of the box, some of it after you have installed third-party components.
This session covers the types of additional code that can be loaded in a Python session.
Modules are Python files containing reusable code (e.g. functions, constants, utilities).
Packages are collections of modules.
Libraries, technically, are collections of packages, although packages and libraries are often used loosely and interchangeably in Python.
Python comes with an extensive standard library. As soon as you launch the program, you can access part of it such as the built-in functions and built-in constants:
Example:
Most of the standard library however is held in several thematic modules. Each module contains additional functions, constants, and facilities. Before you can use them, you need to load them into your session.
The math module contains many mathematical functions and constants, including the sqrt
function.
This function cannot be accessed directly:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 1 ----> 1 sqrt(9) NameError: name 'sqrt' is not defined
In order to use it, you have two options:
You can create an alias for the module:
This is particularly convenient with modules of longer names.
You can install external packages containing additional functions, constants, datasets, and utilities to extend the capabilities of Python.
The Python Package Index is a public repository of open source packages contributed by users.
Installation of packages can be done via pip or (better and much faster) with the new Python package and project manager uv.
Instead of installing packages system wide or for your user, you can create a semi-isolated Python environment in which you install the packages needed for a particular project. This makes reproducibility and collaboration easier. It also helps handle dependency conflicts. It is a great practice to always use virtual environments.
Some Linux distributions will not let you use pip
outside a virtual environment anymore.
uv
will automatically install packages in virtual environments.
Note that the method with uv
in the next section is a better (and much faster) option.
Create a Python virtual environment called env
:
Activate it:
Update pip:
Install packages:
To deactivate a virtual environment, run:
uv is an amazing new package and project manager for Python written in Rust that replaces all the old tools (pip
, pyenv
, virtualenv
, poetry
, pipx
, etc.). It does everything very well and very fast.
The best approach while using uv
is to create projects. In addition to a virtual environment, a project contains a TOML file with a list of dependencies (Python packages), a file setting the Python version for the project, and a lock. This is extremely convenient.
Create a new project:
Install packages in the virtual environment of the project:
You don’t need to activate the virtual environment as long as you are using uv
commands.
If you want to launch Jupyter from a uv
project, run:
If you already have a workflow involving pip
that you cannot change and you still would like to use uv
, you can use the pip interface. It is not as convenient as the project approach, but it allows you to benefit from uv
speed.
For more information on uv, you can have a look at the webinar I gave on this tool in May 2025.
While uv
is fantastic to handle all things Python-related (Python versions, packages, environments, projects, etc.), if you need to install a software stack involving non-Python dependencies on your local machine, you will need to use Mamba or conda.
Don’t use conda or Anaconda on the Alliance clusters. If you really must, do it in a container with Apptainer.
On the Alliance clusters, install packages inside a virtual environment and use Python wheels whenever possible.
You can see whether a wheel is available with avail_wheels <package>
or look at the list of available wheels. To install from wheels instead of downloading from PyPI, add the --no-index
flag to the install command.
Advantages of wheels:
The workflow thus looks like:
At this point, uv
is not technically supported on Alliance clusters. Some people have had success using it, others have had issues while interacting with the clusters module system.
You can run either of the pip
or uv
commands from within a Jupyter notebook cell by preceding them with the IPython magic command !
which lets it know that it is a Bash (rather than a Python) command.
Example: