How to create 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 3 is a versatile language, you can run it on Windows 10 as well.
In case you need to run your Python 3 application in its own virtual environment in Windows 10, this post is for you.
Determining the Python 3 version to install
When you head over to the Python 3 download page for windows, you will find several options:
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 will download Windows x86-64 executable installer for Python 3.7.1 runtime.
Choosing the Python 3 features to install while running the Python 3 installer
After you had downloaded the Python 3 installer, double-click on it. Choose to Add Python 3.7 to PATH:
Left-click on Customize installation. After the next screen appears, check pip:
Left-click on Next and the Advanced Options screen appears:
Finally, left-click on Install to start the installation progress:
When the installation had completed, you will see the following screen:
Left-click on Close to exit the installation wizard.
Creating a virtual environment on Windows 10 with Python3 venv module
Once you had completed the installation of Python 3 on Windows 10, you will be ready to create the virtual environment for your 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 for your isolated virtual environment.
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:
activate.bat script had ran, you will see the prompt appended with (my-venv):
This tells us that the command prompt session is inside the Python 3 virtual environment.
Installing Python 3 dependencies into your Python 3 virtual environment on Windows 10
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
Running your Python 3 application inside your Python 3 virtual environment on Windows 10
Subsequently, when you had installed all the needed dependencies, you can then run your Python 3 application with the python binary:
Deactivating your 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:
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.