Skip to content

Setting Up A Virtual Environment With Miniconda

What and Why

A 'virtual environment' can be thought of in Python terms as a container for a specific Python version (Python 3.x), and a set of installed packages (such as numpy or scikit-learn). You can have multiple virtual environments installed at once, each containing a different Python version and/or different packages (and versions of them). Why bother? Software changes over time as it updates, and sometimes changes can affect how the software is written, or even the results given by their calculations. Also, packages need to work together, and some will only work with specific versions of others. This situation can become quite complex. Miniconda (or package managers in general) can help deal with these situations in a few ways. Firstly, the virtual environment model means that encapsulated sets of packages can be installed which do not clash with each other. Furthermore, installing only the packages that you really need for project is possible, even if you have several projects with varying requirements. Secondly, Miniconda can figure out which dependencies can or should be installed, given the other packages in the environment so that everything remains compatible.

Check Miniconda Installation

For this guide, you should have installed miniconda (if not, see the install Python guide).

Successful installation of miniconda should provide the conda command in you command line. You can check for this by typing the following, which should print the miniconda version you have installed:

conda --version

If there is an error, you may have to trouble shoot.

Create a Virtual Environment

To create an new virtual environment called 'test-env' containing Python 3.11, run the following:

conda create -n test-env python=3.11

Miniconda will then create the new virtual environment, and give you some information, including how to activate and deactivate it.

Activate

To activate a virtual environment:

conda activate test-env

You can check which environment you are in by running conda env list. The environment which is currently activated will have a * next to it. The 'base' environment is the default environment which is activated when your terminal starts.

To deactivate a virtual environment:

conda deactivate

You can probably see the effect this has by running python --version with and without the environment activated.

Installing Packages in a Virtual Environment

You can install packages into a virtual environment using conda install <package> or pip3 install <package>. pip3 is the command to call pip, Python's own package manager. Miniconda can keep track of pip installations, but prefer using conda based installation where possible.

To install a package, make sure the environment is activated, then, for example, to install numpy, run:

conda install numpy

This will install numpy and its dependencies (other packages and software required to run numpy). You can go on with installing other packages in a similar manner if you like.

If you want to see which packages are installed, run:

conda list

To see only pip installed packages, you can also run:

pip3 freeze

Recording a Virtual Environment

Communicating which Python version and software were used in your software is important in ensuring your work is reproducible and others can easily recreate your virtual environment. You can communicate installed dependencies manually (e.g. by making note of all your conda install and pip3 install commands). A more comprehensive, automated way is to create and file called environment.yaml which contains all of the details of your environment:

conda env export > environment.yaml

(Note: you may wish to open the file and delete the line containing the path to your local installation on your computer).

This file can then be used by others to recreate your installation.

A Note on Security

Most of the core packages we use are widely used and maintained, meaning that they can be trusted. However, there are increasing instances of malware distribution which can get onto your computer via installation of Python packages. For example 'typo squatting' relies on the user typing the wrong package name (maybe something like pip3 install nmpy) which leads to the installation of malware. The maintainers of these software repositories try to prevent these attacks, but obviously nothing is always 100% effective. The software we recommended in these guides should be safe as long as a new version is kept up to date, and you are not writing software which requires proper security (i.e. it processes data, and does not contain your bank account or something). Make sure to type carefully, do some research into what you are installing, and ask for advice if unsure.