Kamal Hosen
Kamal Hosen
Geospatial Developer | Data Science | Python

Nov 07, 2022

Creating a python virtual environment in Anaconda

/media/article-img/anaconda-venv.png

A virtual environment in python is like a tool that helps to manage python packages and third-party libraries. Anaconda is the world's favorite package management tool that helps to create a python version-specific virtual environment. It automates the third-party package/libraries installation process by checking compatibility and resolving dependencies with another package.

In this tutorial, I will show you how to create a virtual environment in Anaconda. Let’s start-

 

Step -1: Check Python and Conda Version

# Checking python version 
python --version

# Checking conda version
conda -V

 

Step -2: Change the directory or folder

# Using cd command navigate to the folder where you want to create a virtual environment
cd < Name of Your Directory/Folder >

 

Step -3: Create a virtual environment specifying the python version

For this tutorial, we create a virtual environment name python_demo_env for python version 3.8.

# conda create -n < Name of the Virtual Environment > python=x.x anaconda
conda create -n python_demo_env python=3.8 anaconda

You have just created a python3 virtual environment.

 

Step -4: Activate the virtual environment to install packages

# conda activate < Name of the Virtual Environment >
conda activate python_demo_env

 

Step-5: Install Packages

Now, you can install any Python package inside this virtual environment. We can use the pip or conda command to install packages. Let’s install Django, the most popular python framework, inside our python_demo_env.

# Install Django using conda command
conda install -c anaconda django

# Install Django using pip command
pip install django

You can also specify the package version during the installation. For example, pip install django==3.2.7

Step -6: Deactivate Virtual Environment

conda deactivate

Step-7: To remove/uninstall package

#conda remove -n envname package_name
conda remove -n python_demo_env django

It will remove the Django package from our virtual environment.

Share To

About Author
  • Kamal Hosen
  • Kamal Hosen
    Geospatial Developer | Data Science | Python

    A passionate geospatial developer and analyst whose core interest is developing geospatial products/services to support the decision-making process in climate change and disaster risk reduction, spatial planning process, natural resources management, and land management sectors. I love learning and working with open source technologies like Python, Django, LeafletJS, PostGIS, GeoServer, and Google Earth Engine.