How to List Virtual Environments in Python

Migel Hewage Nimesha Feb 02, 2024
  1. List Virtual Environments in Python
  2. Conclusion
How to List Virtual Environments in Python

A virtual environment is an environment where we can install libraries, packages, scripts, and a Python interpreter. If your projects require different versions of different libraries or Python interpreters, you can create separate virtual environments for each project.

These virtual environments isolate from other virtual environments that have developed. So the errors related to versions of libraries, packages, and others will not occur.

For example, let’s assume you’re working on a Python project called Project1. It requires version A of the NumPy library.

While working on Project1, you want to start Project2. But it requires version B of NumPy.

So, if we upgrade/downgrade NumPy to version B, Project1 might throw some errors since the needed version for NumPy has changed.

To overcome this, we can create two virtual environments and install those versions separately in them. There are several methods to create virtual environments, but this article will discuss approaches to list the virtual environments.

List Virtual Environments in Python

If we have virtual environments and we want to list all our virtual environments, we can list them using several commands. It is not necessary to activate the virtual environment to list it.

Once created, we can list them down. In this article, we discuss three methods.

Use the lsvirtualenv Command

We can use the lsvirtualenv command to list existing virtual environments. To use this command, we need to install virtualenvwrapper.

The virtualenvwrapper is a tool where we can wrap/box virtual environments. It almost acts like a container.

The system will not identify the command if this tool is not installed.

To list virtual environments, we need to create them. So, let’s create a virtual environment using the virtualenv tool.

The virtualenv is a tool where we can create Python virtual environments that are isolated from each other. There are different methods to create virtual environments, but let’s use the virtualenv tool for this method.

Installing the virtualenv tool (only if you haven’t installed it):

$ pip install virtualenv

Creating a virtual environment:

Syntax:

virtualenv <environment_name>

Now let’s create vEnv1 as our virtual environment.

virtualenv

You’ve successfully created a virtual environment. You can check the path for your virtual environment folder that contains primary executable files to run a Python project.

Installing the virtualenvwrapper tool (only if you haven’t installed it):

$ pip install virtualenvwrapper

Listing virtual environments:

$ lsvirtualenv

Now we can see the virtual environment we created.

Output:

lsvirtualenv

Sometimes you’ll get a message indicating File not found or an empty space. You should change the WORKON_HOME path to your current virtual environment directory.

The default path for WORKON_HOME is \Users\%USERNAME%\Envs. So when we execute the lsvirtualenv command, it will check the path to list the virtual environments.

If you created your virtual environment elsewhere, you can’t get the list, and you’d need to set that path to the WORKON_HOME variable.

  1. Go to Environment variables in your system.
  2. Under System variables, click the New button.
  3. Add WORKON_HOME as the variable and your path to the virtual environment root as the value.
  4. Click OK.

Now, if you try the lsvirtualenv command, you can see a list of virtual environments from where you created them.

Use the Conda Command

Conda is an environment management system that comes with Anaconda and Anaconda is a Python distribution used for data science calculations and package management. Conda has several commands to create, remove and list virtual environments.

If we make our virtual environments using conda, we should list them using the conda command.

Let’s create two virtual environments using conda.

Syntax:

conda create --name <environment_name>

Creating two virtual environments:

$ conda create --name vEnv2
$ conda create --name vEnv3

Now we can list them. Conda provides three commands to list virtual environments. Any of these will give you the same result.

$ conda env list
$ conda info --envs
$ conda info -e

Let’s use the first command, conda env list. As a result, it will display the virtual environments we created, as below.

conda env list

Use the workon Command

We need to install the virtualenv tool to use this command. As mentioned earlier, it can use to create virtual environments, and the workon command comes along with it.

We use the workon command to activate a virtual environment. Also, using this command without arguments can acquire a list of virtual environments.

Since we created a virtual environment using the virtualenv command earlier, let’s try to list it using the workon command.

$ workon

Now we can see the virtual environment we created.

workon

Conclusion

This article discusses a virtual environment and three methods to list virtual environments in Python. You can use other methods, but the developers often use the discussed techniques.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

Related Article - Python Virtual Environment