Creating Python Virtual Environments with virtualenv on Windows

Creating Python Virtual Environments with virtualenv on Windows
4 min read
14 September 2023

Introduction:

Python virtual environments allow you to isolate your Python projects. It enables you to install different versions of dependencies required by different projects.

In this article, we’ll guide you through the process of setting up a virtual environment using virtualenv on Windows.

A Python virtual environment is a self-contained directory that contains a Python interpreter and all the dependencies and libraries specific to a particular project.

It allows you to have multiple isolated Python environments on the same system, each with its own installed packages and versions.

Use of Virtual Environment:

1. Dependency management:
Virtual environments help manage project dependencies efficiently. Each project can have its own set of dependencies without conflicts between different projects. This ensures that each project has the specific versions of libraries it requires, avoiding version compatibility issues.

2. Isolation:
Virtual environments provide isolation between different projects. When working on multiple projects, each with its own requirements, you can create a separate virtual environment for each project. This way, changes made to one project’s environment do not affect the others, providing a clean and controlled environment for each project.

3. Reproducibility:
By using virtual environments, you can precisely replicate the environment in which a project was developed. This allows you to share the project with others or deploy it to different systems while ensuring that the same versions of dependencies are used consistently.

4. Easy project setup:
Virtual environments simplify project setup for collaborators or new team members. By sharing the project’s virtual environment configuration file (e.g., requirements.txt or environment.yml), they can quickly create an identical environment and start working on the project without manual dependency installations.

5. Testing and deployment:
Virtual environments play a crucial role in testing and deployment workflows. They allow you to create an isolated environment specifically for running tests or deploying your application. This ensures that the application’s behavior is consistent across different environments and platforms.

Pre-requisites:

Please confirm that you have installed the latest version of Python on your machine. To check the version installed on the machine use command $ python –version

Steps to install Python on your windows:

  1. Visit the official Python website: https://www.python.org/.
  2. Click on the download link for the Windows installer that corresponds to your system architecture (32-bit or 64-bit). Most modern systems are 64-bit, so you’ll likely want to choose that option unless you have a specific reason to use the 32-bit version.
  3. Once the installer file is downloaded, run it by double-clicking on it.
  4. In the installer window, make sure to check the box that says “Add Python x.x to PATH” (where x.x represents the specific version number you downloaded). This option will add Python to your system’s PATH environment variable, allowing you to run Python from any command prompt.
  5. Click on the “Install Now” button to begin the installation process.
  6. The installer will copy the necessary files and set up Python on your system.

How to create Python Virtual Environment:

Step1: Check if virtualenv is already installed on your machine by using the command $pip show virtualenv

Creating Python Virtual Environments with virtualenv on Windows

Step2: Install virtualenv: Run the command ‘pip install virtualenv‘ to install virtualenv.

Creating Python Virtual Environments with virtualenv on Windows

Step 3: Create a folder for the new virtual environment using the command: $mkdir Name_of_your_directory

Set4: Open New created folder in the command prompt.

Step5: Create a new virtual environment by using the command: Python -m venv <your_directory_name>

Creating Python Virtual Environments with virtualenv on Windows

Step6: Activate the virtual environment by using the command : C:\Path_to_your_virtualenv\Scripts\activate.bat

To deactivate the virtual environment, use the command ‘deactivate

Creating Python Virtual Environments with virtualenv on Windows

Installing Packages in Virtual Environments

After activating a virtual environment, you can use pip to install packages. The packages installed within the virtual environment are separate from the global Python installation.

Sharing Environment Specifications

To share the environment specifications of your project, you can use a requirements.txt file or a more advanced tool like pipenv, which combines virtual environment management with dependency tracking.

Managing Virtual Environments with pipenv

pipenv is a powerful tool that simplifies package management and virtual environment creation. It automatically manages the virtual environment and tracks project dependencies in a Pipfile.

Conclusion:

Python virtual environments are an essential tool for managing dependencies and isolating projects. By following the steps outlined in this article, you can easily set up and work with virtual environments using virtualenv on Windows.

We at Varseno, provide Product Development Services for all business websites to improve their reliability, scalability and stability. Reach out to us for any web application development services.

www.varseno.com

 

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Suraj Patil 2
Joined: 8 months ago
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up