The Ultimate Guide to Streamlining Your Python Workflow with Virtualenv and VirtualenvWrapper - Linux and Mac OS

The Ultimate Guide to Streamlining Your Python Workflow with Virtualenv and VirtualenvWrapper - Linux and Mac OS

Virtualenv and VirtualenvWrapper are two tools that are commonly used in Python development. 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: For Mac OS and Linux

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

source myenv/bin/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 shell 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 terminal and type the following command:

pip install virtualenvwrapper

After installation, you need to configure the shell to use VirtualenvWrapper. Open your shell profile file (.bashrc or .bash_profile) and add the following lines:

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

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. Finally, the source command loads the VirtualenvWrapper script.

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

Did you find this article valuable?

Support Matthew Hard by becoming a sponsor. Any amount is appreciated!