Conda environment
Conda is the environment manager (and package manager) bundled with Anaconda, which is a commonly used distribution of Python, R and other applications. See these instructions on how to install and setup Anaconda. Python packages (and other applications) in Conda are curated by the Anaconda team, however, there is also "conda-forge" which is a "channel" of their package repository which is managed by "the community", i.e. whoever made the packages.
You can interact with conda via the Anaconda Prompt for Windows, or in a terminal window for Mac/Linux. By inputting commands in the command prompt you can then create and manage your virtual environments using the conda package manager.
See the Anaconda user guides for more information on getting started with conda.
How to create a new virtual environment using conda
To create a new conda virtual environment for your project, open the Anaconda Prompt (Windows) or a terminal window on Mac/Linus and enter:
- The
--name
tag specifies a name for the environment: in this example the environment will be named "myenvironment", but you can replace this will something better suited to your project. python=3.9
specifies Python version you wish the virtual environment to run, in this case version 3.9.
To check the packages that are installed in the active environment, enter:
To install a package the active environment:
To create an environment, specify the Python version and install multiple packages in one line:
Notice how the versions for pandas and flake8 are specified but no version is given for numpy. This will result in all versions of numpy being installed.
- TIP: It is recommended to install all packages in one go (e.g. numpy, pandas, pytest etc). Installing 1 package at a time could cause potential package dependency conflicts (see Dependency Hell).
If you're unsure a specific package is installed in the current environment simply search:
For more information on managing environments and other commands such as updating your environment's packages, check out Managing environments with Conda.
How to activate an environment
The 'active' environment is the one that conda will reference when you enter any commands, e.g. new packages will be installed into the active environment, the installed package list will be based on the active environment, etc.
When you start a new Anaconda Prompt (Windows) or open a new terminal (Mac/Linux), the active current environment is set to base
.
To activate an environment, enter:
As above, "myenvironment" specifies the name of the environment and can be replaced with the specific name of the environment you wish to activate.
How to export and share environments
To ensure reproducibility, it is important that we can export virtual environments and share them alongside the code. In this way, someone else will be able to run your code with exactly the same environment (packages, dependencies etc) as you did. This helps address the classic 'it works on my machine' problem.
There are a few options for how to export environments and recreated environments from exported files.
Pip requirements.txt
If you followed the Project structure and package organisation guide, you will have created a requirements.txt
file in your repository, which specifies all the Python packages you wish to install.
- The benefit of a requirements.txt file created with Pip is that anyone with a Python installation should be able to install it (i.e. someone else wouldn't need to have conda installed)
- The drawback is that the requirements.txt file offers an incomplete specification for the environment (for example, it does not specify the Python version), so projects using requirements.txt files must be careful to specify any additional dependencies in another way (e.g. via a README).
To create a working conda environment using the requirements.txt file, simply follow the conda environment creation commands from above and instead of the simple package installation, first activate the target environment:
And then enter:
Conda environment.yml
Conda offers a way to export and share environments via a yaml file.
- The benefit of this approach is that the output file gives a more complete picture of the dependencies for a project than a requirements.txt file from Pip.
- The drawback is the someone would need to have conda installed to use your project: if you work in an organisation or team that consistently uses conda then this is not as relevant, but it may be more relevant if you want to distribute you project to others who may not use conda.
To export the active environment:
The resulting file can be used to completely rebuild a conda environment:
How to remove an environment
Make sure your environment is not active by typing:
This will take you back to the base conda environment. Then to delete your specified environment:
Conda help command
Type the following command for a list of helpful terminal commands:
See Also
Using Spyder with conda environments
Spyder is a Python IDE that is bundled with Anaconda during installation. It can be tricky to set up Spyder to work with multiple conda environments: see this guide for instructions on how to do this.
Using Docker with Data Refinery
Docker is a container manager that offers many solutions and applications for managing your environments. Currently at NHS Digital we cannot use Docker due to compatibility issues. This may change in the future so we will update the resources on this page accordingly.
External links
- Python virtual environments and package
- Installing Anaconda
- Getting started with Conda
- Managing environments with Conda
- Conda commands cheatsheet
- List of other package managers
- Using up Spyder with conda environments
External Links Disclaimer
NHS England makes every effort to ensure that external links are accurate, up to date and relevant, however we cannot take responsibility for pages maintained by external providers.
NHS England is not affiliated with any of the websites or companies in the links to external websites.
If you come across any external links that do not work, we would be grateful if you could report them by raising an issue on our RAP Community of Practice GitHub.