Set up a Virtual Environment using Conda

Learn how to create and manage virtual environments in Python using conda.
python
virtual environments
Author

Shraddha Anala

Published

May 16, 2023

In this blog post we will see how to create Python virtual environments, install packages in it using conda.

What is a virtual environment?

A virtual environment is an isolated, self-contained runtime environment which enables you to run your Python projects on your local machine.

Why is a virtual environment required?

If you intend to build more than one Python project, you will probably require different packages, or, different versions of the same packages, different Python versions, and other dependencies to execute each project.

Therefore to ensure that you are not mixing up dependencies, and can run your earlier projects reliably, it is recommended to set up virtual environments for each project.

Creating virtual environments

There are a number of tools available for you to create and manage your environments. In this short article, we will take a look at the conda package manager. I’ll list out some other environment management tools, and link respective articles at the end of this post.

What is Anaconda?

Anaconda is a Python/R distribution for data science and machine learning packages. In simple words, anaconda bundles tools required to execute your data science or machine learning projects. Tools include Python (versions), Jupyter notebook, open-source packages such as Pandas, NumPy, TensorFlow, etc.

As I work in the ML domain, I find it extremely convenient to use miniconda, to install packages and manage my environments.

Can’t decide if you should use anaconda or miniconda? Read this article here.

Prerequisite

Ensure you have downloaded and installed conda on your system. Follow the instructions for your machine here.

Verify you have correctly installed conda, by executing the below command in your command prompt or terminal.

conda info

New virtual environment

When creating a new environment, give it a short but descriptive name so you can remember the project the environment was created for, should you need to revisit it later. For example, if you are building a web app using flask, you can name your environment, “flask_app_env” or “flask_web_app” and so on.

Steps

1. Choose a name for your environment and create it by running the below command in your terminal or command prompt. You can specify the Python version you want to install or leave it blank to install the latest version.

# python version 3.8  
conda create --name flask_app_env python=3.8  
  
# latest python version. execute any one of these commands.  
conda create --name flask_app_env python

2. Activate your environment.

conda activate flask_app_env

3. Install a package, say pandas.

conda install pandas

• If you saved a list of packages in a requirements.txt file, you can use this command instead to install them in one go.

conda install --file requirements.txt

4. Verify that the packages and their dependencies have been successfully installed.

conda list

5. Additional: You may find that certain packages aren’t available in the anaconda repository or your current conda channel. You can add additional channels before running the conda install command by searching if those packages exist in other channels, and, add the channel name to your conda config.

# search if the package exists in other channels  
conda search <package_name>  
  
# add the channel to the conda config  
conda config --add channels <channel_name>

If there are packages missing from the conda repository entirely, you can install these packages using pip.

pip install <package>

6. Alternate: You can create an environment, and install packages in it, by running a single conda command. First create a YAML file (env.yaml), specify the environment name and packages in it.

env.yaml
name: flask-app-env  
channels:  
 - defaults  
dependencies:  
 - python=3.9  
 - beautifulsoup4==4.11.2  
 - pandas==1.5.3  
 - requests==2.28.1  
 - python-dotenv

Then run this command,

conda env create --file env.yaml

Refer to the conda cheetsheat whenever you need to look up a command to perform a task.

You should have successfully created an environment and loaded your packages in it using conda.

Alternate Tools

Managing environments can be accomplished using a variety of tools such as pipenv, poetry etc. Here are links to these alternate tools as promised!

Pipenv & Virtual Environments

Poetry

I’ve found poetry to be slightly advanced; if you’re a beginner, I would recommend using pipenv or conda, but don’t hesitate to dive into poetry to try it.

Hope you found this article helpful. Happy Programming!

Back to top