Creating Virtual Environment in Python for Windows

Creating Virtual Environment in Python for Windows

Creating a Virtual environment in Python is the best way to manage the dependencies for a project. A virtual environment is an isolated environment where we can install and use packages and dependencies specific to the environment.

In this tutorial, we will see how to set up a virtual environment.

Prerequisite

To set up a virtual environment we need to have the latest version of python installed on our device. Head over to the official site for installation.

Creating and activating the virtual environment

We will be using Windows PowerShell for this activity

We will create a new directory for our virtual environment and navigate into that directory. To do this, execute the code below,

mkdir myproject
cd myproject

Now we will create the virtual environment in the directory myproject using the inbuilt python venv module. To do this, execute the code below,

python -m venv env

Now the virtual environment called env is created. To activate this we use below code,

env\Scripts\activate

After executing the above code, the virtual environment env is activated. Now we can install and use packages and dependencies in the created environment required for our project.

To deactivate the virtual environment we type the command deactivate in the PowerShell.


In this post, you have learned how to create, activate and deactivate a virtual environment. If you found it useful, please consider liking and sharing it to help spread the knowledge. Feel free to comment if you are stuck in any step or have any doubts so we can clarify it.