How to Create a Virtual Environment for a Python Project in Windows



Python


In this article, we show how to create a virtual environment for a Python project in windows.

Virtual environments can be essential when you are working with multiple projects on a computer that may require different versions of different modules.

Creating virtual environments allow you to specify the exact requirements and versions of Python modules necessary for a specific project.

Without virtual environments, all Python modules would be the same version, and this may affect or render impossible other projects.

Thus, knowing how to create a virtual environment is essential.

Creating a virtual environment is simple with a few steps.

The first step is that you should go to your Scripts folder in Python and download the virtual environment module.




After this is installed, then what you should do is create a new folder, which will represent where your virtual environment will exist.

For example, I will create a folder named Helloworld with the following line.




With this directory now created, then move into it with the cd command.




Now within this folder, we create a virtual environment, which we will name venv.




After you run this command, if you look into the folder, you should see a venv directory.

This venv directory has a Scripts directory. Inside this Scripts directory are 2 important .bat files named activate.bat and deactivate.bat

Contents of the Scripts directory of a python virtual directory in windows

These are important, as they allow us to activate the virtual environment or deactivate it.

We will see how to do both in this article.

So the next thing we need to activate the virtual environment.

This is done by running the activate.bat file, which is done through the following line of code below.




You will know that the virtual environment is activated when you see (venv) all the way to the left.

How to activate a python virtual environment in windows

You know this is a freshly created virtual environment when you run 'pip freeze' and get no modules returned.

Newly created python virtual environment in windows

This signifies that no modules have been installed yet in this virtual environment.

Now to install a Python module in this virtual environment, make sure that it is activated and then go into the Scripts directory of the venv directory.

This module contains the pip.exe and pip3.exe files needed to install new modules.

So let's install django in this virtual environment through the line, pip install django.

Once this is executed and we run 'pip freeze', we get the following output.

Django module installed in a virtual environment in windows

With django installed, we can now start a django project in the virtual environment. This is shown below.

Starting a new django project in a virtual environment

Now you can create multiple websites on the same computer all with different modules installed for each website.

That is the power of virtual environments. Each is full self-contained and does not affect the other projects.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...