DEV Community

Jima Victor
Jima Victor

Posted on • Updated on

How to make a Virtual Environment in Python for your projects

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For Linux:

$ python3 -m venv envname
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For Linux:

$ source envname/bin/activate
Enter fullscreen mode Exit fullscreen mode

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).

Top comments (0)