Advertisements

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:

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 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:

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.

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:

%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.

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:

python a_python_application.py

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:

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.

Advertisements

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.

Advertisements
Advertisements

Udemy.com Homepage 300x250 Domain names for just 88 cents!

Find a domain starting at $0.88

powered by Namecheap