The Ultimate Guide to Streamlining Your Python Workflow with Virtualenv and VirtualenvWrapper - For Windows

Matthew HardMatthew Hard
4 min read

Virtualenv and VirtualenvWrapper are two tools that are commonly used in Python development on Windows. They provide a way to create isolated Python environments that allow developers to install dependencies without affecting other projects or the global Python installation. In this post, we will take a detailed look at Virtualenv and VirtualenvWrapper and see how they can be used to manage Python environments.

Note: This covers virtualenv and VirtualenvWrapper commands only for the Windows os.

What is Virtualenv?

Virtualenv is a tool for creating isolated Python environments. It creates a self-contained Python installation and allows developers to install packages and dependencies without affecting other projects or the global Python installation. With Virtualenv, developers can easily manage dependencies for different projects and work on multiple projects simultaneously without worrying about version conflicts.

Installing Virtualenv

Virtualenv can be installed using pip, the Python package manager. To install Virtualenv, open a command prompt and type the following command:

pip install virtualenv

Once Virtualenv is installed, you can create a new Python environment using the virtualenv command.

Creating a Virtual Environment

To create a new virtual environment, navigate to the directory where you want to create the environment and enter the following command:

virtualenv myenv

This will create a new directory called myenv in the current directory and install a standalone Python interpreter and pip executable in it. To activate the virtual environment, run the following command:

myenv\Scripts\activate

Once the virtual environment is activated, any packages you install will be installed in this environment and not in the global Python environment. You can deactivate the virtual environment by running the following command:

deactivate

Installing Packages in a Virtual Environment

Once a virtual environment is activated, you can install packages using pip as you would normally do. For example, to install the requests package, run the following command:

pip install requests

This will install the requests package in the virtual environment. You can check the installed packages using the following command:

pip freeze

This will display a list of all the installed packages in the current virtual environment.

What is VirtualenvWrapper?

VirtualenvWrapper is a set of extensions to Virtualenv that provides an easier way to manage multiple virtual environments. It provides a set of commands that allow you to create, delete, and switch between virtual environments with ease.

Installing VirtualenvWrapper

VirtualenvWrapper can be installed using pip. To install VirtualenvWrapper, open a command prompt and type the following command:

pip install virtualenvwrapper-win

After installation, you need to configure the shell to use VirtualenvWrapper. Open your shell profile file (e.g., %USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 or %USERPROFILE%\Documents\WindowsPowerShell\profile.ps1) and add the following lines:

$env:WORKON_HOME = $HOME\Envs $env:VIRTUALENVWRAPPER_PYTHON = 'C:\Python39\python.exe' $env:VIRTUALENVWRAPPER_SCRIPT = 'C:\Python39\Scripts\virtualenvwrapper.ps1' Import-Module virtualenvwrapper

The WORKON_HOME variable specifies the directory where the virtual environments will be stored. The VIRTUALENVWRAPPER_PYTHON variable specifies the path to the Python interpreter that will be used to create the virtual environments. The VIRTUALENVWRAPPER_SCRIPT variable specifies the path to the virtualenvwrapper.ps1 script. Finally, the Import-Module command loads the VirtualenvWrapper module.

Creating and Managing Virtual Environments with VirtualenvWrapper

VirtualenvWrapper provides several commands that allow you to create and manage virtual environments. Here are some of the most common commands:

mkvirtualenv: creates a new virtual environment

workon: activates an existing virtual environment

deactivate: deactivates the current virtual environment

rmvirtualenv: deletes a virtual environment

lsvirtualenv: lists all the virtual environments

To create a new virtual environment using VirtualenvWrapper, simply run the following command:

mkvirtualenv myenv

This will create a new virtual environment called myenv. Once the virtual environment is created, it will automatically be activated. You can then install packages and dependencies using pip as you normally would.

To switch between virtual environments, simply run the workon command followed by the name of the virtual environment you want to activate. For example, to activate the myenv virtual environment, run the following command:

workon myenv

This will deactivate the current virtual environment (if any) and activate the myenv virtual environment. You can then install packages and dependencies for this environment as needed.

To delete a virtual environment, run the rmvirtualenv command followed by the name of the virtual environment you want to delete. For example, to delete the myenv virtual environment, run the following command:

rmvirtualenv myenv

This will delete the myenv virtual environment and remove all its installed packages and dependencies.

Overall, Virtualenv and VirtualenvWrapper are powerful tools that make it easier to manage Python environments and dependencies on Windows. By creating isolated environments for each project, developers can avoid version conflicts and ensure that their projects are running on the correct versions of packages and dependencies.

0
Subscribe to my newsletter

Read articles from Matthew Hard directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Matthew Hard
Matthew Hard

I'm Matthew, a cybersecurity enthusiast, programmer, and networking specialist. With a lifelong passion for technology, I have dedicated my career to the world of cybersecurity, constantly expanding my knowledge and honing my skills. From a young age, I found myself captivated by the intricate workings of computers and networks. This fascination led me to pursue in-depth studies in the fields of networking and cybersecurity, where I delved deep into the fundamental principles and best practices. Join me on this exciting journey as we explore the multifaceted world of technology together. Whether you're a beginner or a seasoned professional, I am here to share my knowledge, discuss the latest trends, and engage in insightful discussions. Together, let's embrace the ever-changing world of tech and navigate the complexities of cybersecurity with confidence and expertise.