Skip to content

Python Environments

Python environments are isolated spaces where specific versions of Python along with its libraries and dependencies can be installed, separate from other projects. They help ensure that different projects on the same machine do not interfere with each other due to differing requirements. Each environment maintains its unique ecosystem, which is essential to managing complex Python projects with differing dependencies.

Python environments are crucial for the following reasons:

  • Dependency management: Environments allow you to work on different projects requiring different versions of Python packages without interference.
  • Replicability: An environment can be exported into a requirements file, which can be shared and used to recreate the same environment on another system.
  • Risk minimization: Any changes or installations are confined to the environment and won't affect your system's Python setup.

There are several tools for managing Python environments, two popular options amongst data scientists being conda and venv.

Creating a Python Environment with conda:

conda is a package, dependency, and environment manager that comes with the Anaconda distribution of Python.

To create a conda environment, use the following command:

conda create --name myenv

To activate the environment: conda activate myenv

To deactivate the environment: conda deactivate

For more details, you can refer to the official conda documentation on managing environments.

Please note, that creating a conda environment is done for you as part of the data science cookiecutter setup, please see here for more details on how this works in practice!

Creating a Python Environment with venv:

venv is a module provided by Python 3.3 and later to create lightweight, isolated Python environments.

To create a venv environment, use the following command: python3 -m venv /path/to/new/virtual/environment

To activate the environment: source /path/to/new/virtual/environment/bin/activate

To deactivate: deactivate

For more details, you can refer to the official Python venv documentation.

Remember, the best practice is to create a new environment for each of your Python projects to keep your work organised and avoid any conflict between dependencies.