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