type(3) # type is a built-in functionint
Marie-Hélène Burle
This section explains how to access Python modules from the standard library and how to install external packages.
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 several options:
You can create an alias for the module:
This is particularly convenient with modules of longer names. Note that everybody uses the same aliases for packages and modules and it is a good idea to respect these habits as it will make your code easier to read by others.
A very large number of free and open-source packages are created by the community. They expand the capabilities of Python into specialized domains.
You can browse for the in the Python Package Index (PyPI).
Your turn:
Ask Google or an LLM for the name of a statistical plotting library in Python, then look for it in PyPI.
It is a very good idea to install the packages for a project into a virtual environment. This isolates projects from one another and prevents conflicts between dependencies that might arise when you have too many packages installed in the same place.
Additionally, it allows you to have packages for different projects at different versions if that is important for reproducibility.
There are several methods to install packages. Here are the main ones:
| Tool | Description |
|---|---|
| pip | The official method to install Python packages. It requires other tools to manage the virtual environments. |
| uv | A new, efficient, and wonderful tool to manage Python packages, projects and environments. The best tool by far in my opinion! |
| Anaconda | A data science platform for Python that comes with Python itself, the conda package and environment manager, and many packages for data science. This project is more beginner friendly and popular among Windows users. |
| Miniconda | A minimal installation of conda. |
In this course, we will use Anaconda to install packages.
Anaconda comes with a number of packages, but it can be used to install new packages via the conda command.
First, launch a terminal to run conda:
Press the Windows Key, type Anaconda Prompt, and press Enter.
macOS users: open the program called Terminal.
Linux users: open the terminal emulator of your choice.
Now, create a new Python virtual environment by typing in the terminal:
Activate that environment:
Install the package(s) you need. Here is an example for the package polars:
If you want to deactivate the virtual environment (when you are done or if you want to activate a different one), run: