How to make a Virtual Environment in Python

In order to make a virtual environment for your python project, you can use the venv module which is built-in to python.

Here are the steps you need to follow:

Open command prompt

To open command prompt, press the home button on your keyboard, search for it, and press enter.

Create a directory for your python project

The virtual environment needs to be in the same folder as your python project. In order to create a folder for your project in command prompt, you can run the following command:

> mkdir pythonproject

where pythonproject is whatever you want to name your python project.

After running the above command, you need to go inside the pythonproject folder you’ve just created. To do this, you can run the following command:

> cd pythonproject

where cd stands for “change directory”

Create the virtual environment

We can now create the virtual environment by running the following commands

For Windows:

> py -m venv envname

For Linux:

$ python3 -m venv envname

where envname is your name of choice for your virtual environment.

Activate the environment

Run the following code to activate the virtual environment:

For Windows:

> envname\Scripts\Activate

For Linux:

$ source envname/bin/activate

If you’ve done everything correctly, you should see the name of your virtual environment in brackets, in front of your current directory in your command prompt. virtual environment active

The above image shows that you’ve successfully created the virtual environment and it is active.

Another thing you’d notice, is that if you open pythonproject (your project folder) in file explorer, inside of it, there would be another folder named envname (the name of your virtual environment).

Written By

Jima Victor

Code lover, crafting cool stuff with code. Also, geeking out in articles for the tech community. 🚀