Tuesday 1 September 2020

Python Virtual Environments on Windows: A Comprehensive Setup Guide

Are you looking to create Python virtual environments on Windows for your projects? Virtual environments allow you to keep project dependencies separate and manage different Python versions efficiently. In this comprehensive step-by-step guide, I'll show you how to set up Python virtual environments on Windows for smoother development.

Step 1: Install the Desired Python Version

Before creating a virtual environment, ensure you have the Python version you want to work with installed on your system. For example, you can use Python 2.7 or Python 3.6. If you have multiple Python versions, I'll cover how to create virtual environments for specific versions later.

Step 2: Create a New Virtual Environment

Open your command prompt and navigate to the directory where you want to create the virtual environment, let's say D:\MyProject\. Now, use the following command to create a new Python virtual environment:
python.exe -m venv env-dir
This command will create a directory named env-dir inside D:\MyProject\ containing a copy of the Python interpreter, standard library, and necessary supporting files.

Step 3: Activate the Virtual Environment

To start working within the virtual environment, you need to activate it. Use the following command:
.\env-dir\Scripts\activate
You'll notice your command prompt changes to reflect that you are now working within the virtual environment.

Step 4: Managing Python Versions in Virtual Environments

In case you have multiple Python versions installed and want to create a virtual environment for a specific version, use the Python executable from that particular version in the venv command. For example:
C:\Python\Python36-32\python.exe -m venv env36-dir
This will create a virtual environment specifically using Python 3.6.

Step 5: Installing the Latest Version of pip

Before proceeding, check the installed version of pip within the virtual environment using the command:
pip --version
If you need to install the latest version of pip, download the get-pip.py file. Place this file in the D:\MyProject\env-dir\Scripts\ directory.
Open a command prompt in administrator mode, navigate to the same directory, and run the following command:
python get-pip.py
This will install the latest pip version in the virtual environment.

Step 6: Managing Dependencies

To keep track of the dependencies within your virtual environment, you can create a requirements.txt file. While in the existing virtual environment, execute:
pip freeze > requirements.txt
This will create a list of all the installed packages along with their versions in the requirements.txt file.

Step 7: Setting Up Dependencies in a New Environment

To recreate the same environment with all its dependencies in a new virtual environment, first, ensure you've activated the new environment, and then use the following command:
pip install -r requirements.txt
This will install all the packages listed in requirements.txt into the new environment.
Using Python virtual environments on Windows enhances development flexibility and isolates project dependencies from the system-wide installations. Switching between different Python versions becomes a breeze, allowing you to work on multiple projects seamlessly.

Start utilizing virtual environments in your Python projects today and experience the convenience and organization they bring to your development process.