How to create and activate python virtual environment in windows

python virtual environment

It is very important for a beginner programmer to learn to create a virtual environment in python and activate it in this tutorial with step by step guide with pictures.

Python is an interpreted language and there are lots of libraries and modules are available for python. They have different versions. In real-world projects we often use these modules within our programs.

When we have multiple projects using the same module but with different versions, then we really need to manage it via a virtual environment.
Because we can only install a single version of any module in a python package which can be either a global python package or a virtual environment.

First – Open command prompt and navigate to the directory where you need to create virtual environment

Note : Make sure you have added global python’s path to the environment variables.

Second – Upgrade setuptools and pip with the following commands (optional but recommended).

upgrade pip

python -m pip install --upgrade pip
pip install --upgrade setuptools

Then – Install “virtualenv” library to your python for creating virtual environment by following command.

pip install virtualenv

Then – After downloading and installing “virtualenv” library use the following command to create your virtual environment.

virtualenv envname

Finally – Now you have created a python virtual environment. You need to activate the environment to install libraries and run your program with that environment.

Use the following commands to navigate the activation script and activate the environment in the given order. The steps are attached as a screenshot.

create and activate virtual env

cd envname
cd scripts
activate

At this point you will see (envname) in brackets. If yes then your environment is activated successfully.
Now you can install libraries in the new environment and navigate where you need to run your program.

Thanks for reading the post “create a python virtual environment”. To read more go to the official website of python here.
Also read, How to host a Django web application on IIS.

Related posts

Leave a Comment