Creating Virtual Environment using Anaconda Prompt & Activating it in Vs Code Editor

Creating virtual environment using conda and other useful commands

In Anaconda Prompt, you can create a virtual environment with a specific Python version using the conda create --name django-app python=3.8 command. This command will create a new virtual environment named django-app with Python version 3.8. You can change 3.8 to any other desired Python version. After running the command, you will be prompted to proceed. Type y to confirm and proceed with the installation.Keep on reading to learn other useful commands.

Checking list of virtual environments

You can check list of environments in conda using the command conda env list:

(base) C:\Users\codingissimple>conda env list
# conda environments:
base                  *  C:\Users\codingissimple\anaconda3
django-app               C:\Users\codingissimple\anaconda3\envs\django-app
flask-app                C:\Users\codingissimple\anaconda3\envs\flask-app

Activating virtual environment

Once the virtual environment is created, you can activate it using conda activate django-app command. Replace django-app with the name of your virtual environment.

Once you activate the newly created virtual environment, you can check the python version by using the command python --version

Deactivating virtual environment

To deactivate the virtual environment and return to the base Anaconda environment, you can run the command conda deactivate

Deleting virtual environment

To remove new environment created and also all of its associated packages you can run conda env remove --name django-app command replace django-app with your virtual environment name.

(base) C:\Users\codingissimple>conda activate django-app

(django-app) C:\Users\codingissimple>python --version
Python 3.8.18

(django-app) C:\Users\codingissimple>conda deactivate

(base) C:\Users\codingissimple>conda env remove --name django-app

Remove all packages in environment C:\Users\codingissimple\anaconda3\envs\django-app:

(base) C:\Users\codingissimple>conda env list
# conda environments:
base                  *  C:\Users\codingissimple\anaconda3
flask-app                C:\Users\codingissimple\anaconda3\envs\flask-app

(base) C:\Users\codingissimple>

conda –help command will give you list of commands you can run.

Let’s create the django-app virtual environment again with python version 3.8 using the command conda create --name django-app python=3.8 , activate it by running the command conda activate django-app and install few packages in this virtual environment.

Installing python packages in virtual environment

Install django and psycopg2 packages in django-app virtual environment

   conda install django 
   conda install psycopg2

Seeing list of packages in virtual environment

conda list will give list of packages in environment

(django-app) C:\Users\codingissimple>conda list
# packages in environment at C:\Users\codingissimple\anaconda3\envs\django-app:
# Name                    Version                   Build  Channel
asgiref                   3.5.2            py38haa95532_0
backports                 1.1                pyhd3eb1b0_0
backports.zoneinfo        0.2.1            py38h2bbff1b_0
ca-certificates           2023.08.22           haa95532_0
django                    4.1              py38haa95532_0
libffi                    3.4.4                hd77b12b_0
libpq                     12.15                h906ac69_0
openssl                   3.0.11               h2bbff1b_2
pip                       23.2.1           py38haa95532_0
psycopg2                  2.9.3            py38h2bbff1b_1
python                    3.8.18               h1aa4202_0
python-tzdata             2023.3             pyhd3eb1b0_0
setuptools                68.0.0           py38haa95532_0
sqlite                    3.41.2               h2bbff1b_0
sqlparse                  0.4.4            py38haa95532_0
tzdata                    2023c                h04d1e81_0
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
wheel                     0.41.2           py38haa95532_0
zlib                      1.2.13               h8cc25b3_0

you can also use pip command in conda virtual environment, to install list of packages mentioned in requirements.txt you can run the command pip install -r requirements.txt or to install packages individually you can run pip install django

Installing multiple packages at once

You can also install multiple packages at once

conda install django-crispy-forms django-allauth djangorestframework python-dotenv django-cors-headers django-debug-toolbar django-extensions django-redis-cache  django-celery django-guardian django-haystack django-storages

These are Python packages often used in a Django project to add various functionality to the web application.

Uninstalling packages

If you need to uninstall django or any other package, you can use conda remove package-name or pip uninstall package-name, replace package-name with the name of the package you want to uninstall , to update all the packages you can run conda update --all

Activating virtual environment in vs code editor

To select this newly created django-app virtual environment for your django project in vs code first make sure you have installed python extension for your django project.

python extension

Open your project folder in vs code, next open the bash terminal and run the following commands

codingissimple@LAPTOP ~/Desktop/my-django-app
$ which python

codingissimple@LAPTOP ~/Desktop/my-django-app
$ conda activate django-app
bash: conda: command not found

As you can see, you can’t activate the conda created virtual environment using the command conda activate django-app . Close the bash terminal and press ctrl+shift+p shortcut key then pop up will appear search Python: Select Interpreter in it and click on it once found.

python interpreter

Now, you will see list of virtual environments choose the one you want , in this case we will choose django-app virtual environment. If you open new terminal now you will see the virtual environment gets activated. As we have changed the interpreter to choose django-app virtual environment, conda related commands should also work.

You can directly activate the virtual environment by first opening a terminal in vs code then run the command source C:/Users/codingissimple/anaconda3/Scripts/activate django-app , replace codingissimple with the windows username and django-app with virtual environment name. I’m assuming default location of installation of anaconda is the same for you.

codingissimple@LAPTOP ~/Desktop/my-django-app
$ source C:/Users/codingissimple/anaconda3/Scripts/activate django-app
codingissimple@LAPTOP ~/Desktop/my-django-app
$ which python

I hope this article has helped you in creating virtual environment using anaconda prompt , activating it for your python project in vs code.

Don't Miss Out! Subscribe to Read Our Latest Blogs.

If you found this blog helpful, share it on social media.

Subscription form (#5)

Leave a Comment

Pin It on Pinterest

Scroll to Top