Create a Python virtual Environment on Windows.

This article shows how to create a Python Virtual Environment on Windows. You can also create a Virtual Environment on macOS.

Open a Windows Command Prompt.

Create a folder for your project:

C:\Users\john\Documents>md exceltest

Navigate to the folder:

C:\Users\john\Documents>cd exceltest
C:\Users\john\Documents\exceltest>
      

Create the virtual environment

Execute the Python command to create a virtual environment:

C:\Users\john\Documents\exceltest>py -m venv env

A folder env is created. The env folder contains a copy of your Python binary and will contain the packages we install in it. To remove the project, including all packages you installed, just remove the project folder including the env folder.

Here you see the directory listing:

C:\Users\john\Documents\exceltest>dir
Directory of C:\Users\john\Documents\exceltest

03/10/2021  09:18 AM              .
  03/10/2021  09:18 AM              ..
    03/10/2021  09:18 AM              env
      0 File(s)              0 bytes
      3 Dir(s)   6,852,497,408 bytes free

Activate the virtual environment

C:\Users\john\Documents\exceltest>env\Scripts\activate

(env) C:\Users\john\Documents\exceltest>

Notice the (env) in front of the prompt. This indicates the activation of the virtual environment.

List installed packages

A new virtual environment has the following packages by default installed:

(env) C:\Users\john\Documents\exceltest>pip list
Package    Version
---------- -------
pip        20.2.3
setuptools 49.2.1

Install packages

To install packages, execute the pip install command:

(env) C:\Users\john\Documents\exceltest>pip install openpyxl
Collecting openpyxl
  Downloading openpyxl-3.0.7-py2.py3-none-any.whl (243 kB)
     |████████████████████████████████| 243 kB 2.2 MB/s
Successfully installed et-xmlfile-1.0.1 openpyxl-3.0.7

You can now use the package by importing it in your code.