| title | How to set up an environment |
|---|
In this guide we summarize some key commands to set up an environment with different tools that you might encounter in the scientific python ecosystem. An environment is a workspace into which you can install Python libraries, separate from what is being used by your operating system.
The environment managers that are covered in this how-to guide include:
- venv
- conda
- mamba
- uv
- pixi
In each of these examples we'll create a new virtual environment related to our
project called science (you can use whichever name you prefer!). We'll activate
the environment, install some dependencies, and see
an example of installing dependencies from an existing file. You may encounter
files like requirements.txt, environment.yml or pyproject.toml that specify
needed dependencies for a project.
With venv to create environment associated with a project folder called science.
python -m venv scienceStart using it by activating it as follows:
source science/bin/activateYou are now ready to install Scientific Python packages using pip! For example:
pip install ipython numpy scipyOften you'll interact with projects that have a specific list of dependencies (for development environments, testing environments, or the project itself). You can install the list of dependencies with pip in your venv using:
pip install -r <path/to/requirements.txt>Remember to re-activate your environment every time you open a new terminal, using:
source science/bin/activateYou can find more information on using venv for packaging here.
With conda, we can create a new environment named science (-n is the same as passing --name):
conda create -n scienceStart using your environment by activating it:
conda activate scienceYou are now ready to install Scientific Python packages using conda!
For example:
conda install ipython numpy scipySome projects distribute environment files with listed dependencies with an environment.yml file.
The first line of this file sets the environment's name. To
create an environment and install the dependencies with this file, use:
conda env create -f <path/to/environment.yml>Remember to re-activate your environment every time you open a new terminal:
conda activate scienceYou can find more information on using conda for environments here.
With mamba, like conda, we can create a new environment named science (-n is the same as passing --name):
mamba create -n scienceStart using your environment by activating it:
mamba activate scienceYou are now ready to install Scientific Python packages using mamba!
For example:
mamba install ipython numpy scipyTo install a specific environment from a .yml file, use:
mamba create -f </path/to/environment.yml>Remember to re-activate your environment every time you open a new terminal:
mamba activate scienceYou can find more information on using mamba in the mamba user guide.
To create a new environment using uv in a project folder called science,
navigate to that folder and execute:
uv venvStart using your environment by activating it:
source .venv/bin/activateYou are now ready to install Scientific Python packages using uv!
For example:
uv pip install ipython numpy scipyTo install dependencies from a requirements file, use:
uv pip install -f </path/to/requirements.txt>Remember to re-activate your environment time you open a new terminal:
source <path/to/science/>.venv/bin/activateYou can find more information on using uv for environments here.
To initialize a new project with pixi in our project called science, execute:
pixi initYou are now ready to install Scientific Python packages as dependencies in this project! From the science directory, execute:
pixi add ipython numpy scipyTo install dependencies from a file like environment.yml, use:
pixi init --import <path/to/environment.yml>Remember to re-activate your environment when you re-open a terminal. Navigate to the science folder, and execute:
pixi shellThis will drop you into the default environment for the pixi project, with all dependencies in that environment accessible to you in that shell.
A pixi project may have multiple environments defined in the pixi.toml file. To
load a specific environment:
pixi shell --environment=<envname>You can find more information on using pixi here.