How to Setup Virtual Environment in Windows Command Prompt

Ifeoma Veronica Nwabufo
3 min readSep 3, 2022

A virtual environment is a way of managing packages and versions in python during development.

This isolates the packages used for a particular project so does not meddle with packages installed globally. More so, it is helpful when you want to pass on projects to others. So, if someone wants to work on a project you are working on, the person can take a list of all the packages you used in your project and installs them quickly.

How to Setup Virtual Environment in Windows Command Prompt

In the Windows command prompt, we can install and use virtual a virtual environment by doing the following:

  1. If you have not installed the virtual environment, install virtual environment by:
pip install virtualenv

2. To create a virtual environment, do:

python -m virtualenv nameofenv

Hence, if I want to name my virtual environment env, then I would do:

python -m virtualenv env

So in the above code, the name of the virtual environment is env. You can name it any name you wish.

3. You then need to activate your virtual environment. You do this by:

nameofenv\Scripts\activate

Since the name of my virtual environment is env, I would do:

env\Scripts\activate

Note that for linux-based terminals, you would rather do:

source nameofenv/bin/activate

You would know that you have activated the virtual environment if you find the name of your environment at the beginning of the path description in your command prompt.

If you want to deactivate your virtual environment, do:

deactivate

When you activate a new virtual environment, you would see that all the other packages you had previously on your computer are not there. You can check the packages and versions in your environment by doing:

pip list

The above lists all the packages you have and their versions. Most likely, you would find only pip, setuptools, and wheel when you first run the command. See below:

Checking packages after activating a new virtual environment

You can now install packages and use pip list to check that they are installed.

So, suppose I install pandas as follows:

The above has installed pandas. Notice, that it has also installed some other dependencies. I can now check the list of my packages to see if pandas has been installed.

Updated pip list

Suppose you want to pass a copy of all the packages and their versions, you can save them in a requirements.txt file by doing the following:

pip freeze > requirements.txt

The above creates a requirements.txt file in your working directory.

The packages from the requirements.txt file can be installed later by doing:

pip install -r requirements.txt

I hope you are now able to install and activate a virtual environment.

Have questions? Do not hesitate to ask!

--

--