Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

3.4 Virtual Environments

One of the first practical problems new contributors encounter is that the same repository may behave differently on different machines. Virtual environments help reduce that problem by isolating project dependencies from the rest of the system.

Why virtual environments matter

Without an isolated environment, installing a package for one project can interfere with another project. Version conflicts become harder to diagnose, and reproducing another contributor’s setup becomes much less reliable.

Virtual environments help by:

Conda environments

Many scientific Python projects use Conda because it manages both Python packages and non-Python dependencies well.

If a repository includes an environment.yml file, the usual workflow is:

conda env create -f environment.yml
conda activate jb-git-tutorial

If the environment already exists and the specification changed, contributors often update it with:

conda env update -f environment.yml --prune

The --prune option removes packages that are no longer listed in the environment specification.

Python’s built-in virtual environments

Some repositories prefer Python’s built-in venv tool instead of Conda. A common pattern looks like this:

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

This approach is lightweight and widely available, though it may require more manual handling of non-Python system dependencies.

Which approach should a project choose?

There is no single best choice for every repository.

Conda is often a good fit when:

venv is often a good fit when:

The important thing is to document one recommended path clearly.

Reproducibility and lock-in

An environment file helps, but it does not guarantee perfect reproducibility forever. Package indexes change, upstream packages release new versions, and operating-system differences remain relevant.

For that reason, it is good practice to:

Docker

Some projects go one step further and package the whole runtime in Docker. This can be useful when:

Docker is powerful, but it adds complexity. For many beginner-friendly repositories, a documented Conda or venv setup is the better starting point.

Good repository habits

For collaborative work, a virtual environment is not just a convenience. It is part of the repository’s social contract: it helps contributors arrive at the same working setup with less trial and error.