How to use a Python 3 virtual environment in Windows 10

When you need each Python 3 application that you are building to run in its own isolated environment, you can turn to virtual environments.

Since Python is available on Windows 10, you can also use virtual environments on Windows 10.

Typically, using a Python 3 virtual environment in Windows 10 involves the following steps:

  1. Installing Python 3 with pip and several features.
  2. Creating a Python 3 virtual environment with Python 3 venv module.
  3. Activating the Python 3 virtual environment.
  4. Installing Python 3 packages that your Python application is using into the virtual environment.
  5. Running your Python 3 application within the Python 3 virtual environment.
  6. Deactivating the Python 3 virtual environment.

1. Installing Python 3 with pip and several features on Windows 10

When you head over to the Python 3 download page for windows, you will find several options:

Python 3 download page for Windows as of 20181115

Next, determine whether your Windows 10 operating is 32 bit or 64 bit. When you have a 32 bit operating system, download the latest executable installer with x86. However, if you have a 64 bit operating system, download the one with x86-64.

For example, if I have Windows 10 64 bit, I can download Windows x86-64 executable installer for Python 3.7.1 runtime.

After you had downloaded the Python 3 installer, double-click on it. Choose to Add Python 3.7 to PATH:

Python 3.7.1 (64-bit) Windows Setup first page

Left-click on Customize installation. After the next screen appears, check pip:

Python 3.7.1 (64-bit) Windows setup Optional Features screen

Left-click on Next and the Advanced Options screen appears:

Python 3.7.1 (64-bit) Windows setup Advanced Options screen

Finally, left-click on Install to start the installation progress:

Python 3.7.1 (64-bit) Windows setup progress

When the installation had completed, you will see the following screen:

Python 3.7.1 (64-bit) Windows setup successful screen

Left-click on Close to exit the installation wizard.

2. Creating a Python 3 virtual environment with Python 3 venv module on Windows 10

When you had installed Python 3 on Windows 10, you can then create the virtual environment for your Python 3 application. In order to do so, open up a command prompt window and type the following command:

python -m venv %systemdrive%%homepath%\my-venv

After the command completes, you will find the my-venv directory inside your home directory. Inside the my-venv, you will find the Python artefacts to work with your virtual environment.

3. Activating your Python 3 virtual environment on Windows 10

Before you can run your Python 3 application inside of your Python 3 virtual environment, you will need to activate it. In order to activate your virtual environment, you will need to run the activate.bat script located inside your virtual environment directory.

For example, to activate the virtual environment inside my-venv, you can run the following command in your command prompt window:

%systemdrive%%homepath%\my-venv\Scripts\activate.bat

After the activate.bat script had ran, you will see the prompt appended with (my-venv):

Windows 10 command shell with a Python 3.7.1 virtual environment activated

This tells us that the command prompt session is inside the Python 3 virtual environment.

4. Installing Python 3 packages that your Python application is using into the virtual environment.

When you had activated your virtual environment, you can then install your Python 3 dependencies into your Python 3 virtual environment on Windows 10. For example, you can install the requests library for your Python 3 application to download a file from a HTTP server or upload a file to a HTTP server:

pip install requests

In order to use the pip command, you need to ensure that you had installed it during your Python 3 installation.

5. Running your Python 3 application within the Python 3 virtual environment

Subsequently, when you had installed all the needed dependencies, you can then run your Python 3 application with the python binary:

python a_python_application.py

6. Deactivating the Python 3 virtual environment on Windows 10

When you want to get out of your Python 3 virtual environment on Windows 10, you can simply run the following command:

deactivate

After the virtual environment is deactivated, your command prompt will switch to the global Python 3 environment. In addition, those Python 3 dependencies that you had installed in your virtual environment will not be available.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.