by Dr Seán Carroll
Coding Environment Setup
8. Python environments
Python environments are an essential aspect of managing your Python projects as they allow you to isolate dependencies and maintain different configurations for each project. This isolation helps prevent conflicts between packages, ensures reproducibility, and makes it easier to share projects with others.
For example, you might have a project that requires an older version of a package, while another project needs the latest version. By using separate environments, you can accommodate both requirements without conflicts or the need to constantly switch between package versions.
Recall that we installed a tool called Conda as part of our Anaconda Distribution installation. This is the tool that we’ll use to manage the creation, activation and deactivation of our Python environments.
Creating and activating a new environment
To create a new environment, open up your terminal and navigate to a convenient location. Your terminal is actually a view into a folder on your computer. You can type ls
to list all of the folders within that current folder, to help get your bearings. From here, change into a convenient directory using the cd
command, in my case I’ll change into my desktop folder with,
cd desktop
Now I’ll create a new folder called Test
in which to work, with the command mkdir
,
mkdir Test
Next, I’ll change into the new folder with cd Test
. I could have done all of this from the Finder (MacOS) or File Explorer window (Windows), but it’s good to get into the habit of working in the terminal.
Now I’m ready to create a new environment with the following command template,
conda create --name my_env python=3.x
We’ll replace my_env
with a name of our choice. Specifying the Python version is optional although it’s a good idea to specify at least python=3
which will make sure the latest version of Python is used within the environment. So for example we can run,
conda create --name test_env python=3.9.7
to create an environment called test_env
that uses Python 3.9.7. We now have an isolated container to work in, with the specified version of Python that will have no interaction with any other projects on our machine.
Next, we need to activate the environment using,
conda activate test_env
You’ll notice the root of your command prompt in the terminal changes from (base)
, your default environment, to test_env
. Now we’re free to install any packages we like within this isolated environment. As you start to work on different projects at the same time, this becomes a huge asset and quality of life improvement.
Installing packages
While the environment is running, you can list all of the packages installed in that environment with the command conda list
. To install a new package in this environment, simply type conda install
followed by the package name. So, for example, we can install NumPy with,
conda install numpy
This will install the latest version of NumPy in our environment. If we want to install a specific version of a package, we just specify the version in the install command, for example,
conda install pandas=1.5
This will install the last 1.5 version of Pandas, which happens to be 1.5.3. (the next release was 2.0). We can remove a package from our environment with the conda remove
command, e.g.
conda remove pandas
Deactivating and deleting environments
To deactivate an environment, simply type conda deactivate
and the currently active environment will be deactivated. This returns us to the default base
environment.
Finally, we may want to delete an environment. We can start by listing all of the environments we’ve created with conda env list
. If any of our environments are active when we execute this command, this will be indicated with an *
. Then, to delete one of the environments from the list, we can use the command template,
conda env remove --name my_env
So, in our case,
conda env remove --name test_env
This covers the basics of environment management using conda. This covers 95% of what we’ll need to do in the terminal with environments. In the next lesson, we’ll fire up Visual Studio Code and find our way around.