2 minute read

TLDR

1
conda create --name my_conda_env python=3.10
1
conda env list
1
conda activate my_conda_env
1
pip install <package_name>

or

1
pip install -r requirements.txt 

What’s a conda environment?

Knowing how to set up a Conda environment is an essential skill for any data scientist or Python developer. Conda is an open source package management system for Python.

In this post, I will show you how to set up a Conda environment for your project, doing this, will help you to easily install and use any dependency you will need.

Create conda environment

Install and validate your anaconda or conda installation

For this tutorial, you will need to install Python and Anaconda, I recommend downloading miniconda, which is a lighter version of Anaconda.

Before creating a conda environment, you need to check if conda is installed in your system. To do this, open your terminal and type:

1
conda --version

You should see something like this:

1
conda 4.12.0  

Now, make sure you have the latest version of conda by running:

1
conda update conda

Create the environment

You are now ready to create your first conda environment. To do this, run:

1
conda create --name my_conda_env python=3.10

To specify the path where you want to create the environment, use the --prefix or -p flag. For example, to create the environment in the default_path directory, run:

1
conda create --prefix <default_path>/my_conda_env python=3.10

Make you created the environment by running the following command:

1
conda env list
1
2
3
4
# conda environments:
#
base                  *  /Users/Jroldan001/opt/anaconda3
my_conda_env             /Users/Jroldan001/opt/anaconda3/envs/my_conda_env

As you can see, the environment you just created is listed in the output, but it is not active yet. To activate it, run:

1
conda activate /Users/Jroldan001/opt/anaconda3/envs/my_conda_env

Now the * symbol is next to the environment you just activated.

1
2
3
4
# conda environments:
#
base                     /Users/Jroldan001/opt/anaconda3
my_conda_env          *  /Users/Jroldan001/opt/anaconda3/envs/my_conda_env

Check python and pip

Now that you created your environment, make sure that you are using the correct python and pip. For this run which python and which pip. You should see the same path as the one you used to create the environment.

Install packages using pip

Now that you have your environment set up, you can install any package you need. For this, you can use pip. For example, to install numpy run:

1
pip install numpy

You can check that the package was installed by running:

1
pip freeze

And you will see the following

1
numpy==1.25.2

Install packages from a requirements.txt file

If you have a requirements.txt file, you can install all the packages listed in it by running:

1
pip install -r requirements.txt 

You can check that all packages were installed by again running the pip freeze command.

Deactivate the environment

To deactivate the environment, run:

1
conda deactivate

As you will see, the * symbol is now next to the base environment.

1
2
3
4
# conda environments:
#
base                  *  /Users/Jroldan001/opt/anaconda3
my_conda_env            /Users/Jroldan001/opt/anaconda3/envs/my_conda_env