My favorite virtual environment manager!

During the last 5 years, I have tried out several virtual environment managers for Python. I will list some of Python's virtual environment managers in the order I have used them.

Python's virtualenv

Python has its own native way of managing virtual environments. It is the bare bones of many virtual environment management tools out there. To use it make sure that you have virtualenv installed in your system. You can install it via pip :

>>>python3 -m pip install --user virtualenv

Now, to create a virtual environment, you run the following command:

>>>virtualenv venv

This command creates a virtual environment named venv. Of course, you can name it anything but venv is one of my go-to names. Once you have installed your virtual environment, you can activate it and install the necessary packages into it.

>>>source venv/bin/activate
(venv)>>>pip install numpy

You can come out from this virtual environment using the following command:

(venv)>>>deactivate
>>>

This whole process is certainly not beginner friendly and many shy away from using virtual environments.

Conda

Conda is popular among AI practitioners. Conda is an open-source virtual environment manager as well as a package manager. It facilitates creating and managing virtual environments. Unlike virtualenv, Conda environments can be activated and deactivated from anywhere. To use Conda, you must first install Anaconda or Miniconda. Anaconda is a distribution of Python targetted especially for AI\DS\ML. It comes preinstalled with many popular libraries for AI\DS\ML. Miniconda is a lighter version of Anaconda. You can download and install it from here: https://www.anaconda.com/, https://docs.conda.io/en/latest/miniconda.html.

After you have installed Conda using either one of these, you can create a new environment using :

(base)>>>conda create -n venv

Notice that you might see (base) in your terminal. This is expected in most of the shells after installing Conda. It just means that the name of your default virtual environment is base.

It is also easy to install a virtual environment with different versions of Python. To do this, you simply run:

(base)>>>conda create -n venv python=3.10 # you can install other versions too

Running this will create a new virtual environment named venv with Python 3.10.

To activate this environment you can run the following command from anywhere:

(base)>>>conda activate venv
(venv)>>>

To deactivate it, you can just run:

(venv)>>>conda deactivate
(base)>>>

pipenv

pipenv is a combination of pip and virtualenv. It is both a virtual environment manager and a package manager like Conda. pipenv deserves its own article but I will try to discuss its pros and cons very quickly. pipenv has a better way to resolve dependencies for packages using dependency graphs. pipenv produces two files called Pipfile and Pipfile.lock, which are used to create an exact copy of the virtual environment everywhere. However, the dependency resolution methods used pipenv are buggy and prone to errors.

You can install pipenv using pip:

>>>pip install pipenv

To initialize a new environment you just run:

>>>pipenv shell

This will create two files Pipfile and Pipfile.lock and will create and activate a virtual environment in a pre-defined path. If the environment already exists for this particular project (because pipenv will find Pipfile in the directory), it will just activate the environment and install dependencies from Pipfile. Consider these files are like requirements.txt files with dependency-resolving features.

Now instead of using pip to install packages, you can just run:

pipenv install pandas

One added feature of pipenv is its --dev flag. Sometimes packages like pytest are only required for development and are not required for production. In such a case, you may want to use the --dev flag while installing such packages. It will put these packages into dev a section of Pipfile and make sure that when you run the pipenv shell command, these packages will not be installed.

pipenv install pytest --dev

virtualenvwrapper

As the name suggests, it is a wrapper virtualenv that we discussed at the beginning of this article. It is just the virtual environment manager and not a package manager. It is one of my favorite environment managers because it is very lightweight and does most of the job for most projects. It facilitates creating, managing, and activating Python virtual environments from anywhere like Conda. Installing it is not intuitive, but you can follow the tutorial here: https://virtualenvwrapper.readthedocs.io/en/latest/

You can create new environments using:

>>>mkvirtualenv venv
(venv)>>>

This will create a new virtual environment with default Python. You can also give the path of the Python interpreter to install specific Python versions. You need to have these versions installed in your system, unlike Conda which allows you to install any version of Python from the web.

>>>mkvirtualenv -p [path/to/python] venv 
(venv)>>>

To deactivate and activate the virtual environment you just run the following command:

(venv)>>>deactivate
>>>workon venv # to activate venv from anywhere
(venv)>>>

Whew! Those were quite a few environment managers. But I still want to discuss one more tool called poetry! It deserves special attention and I will discuss it in the next part of the virtual environment series.