Python virtual environment


What is a Python virtual environment?

Most Python projects we develop will have dependencies, that’s code written by other people. We can install and use them so that we don’t have to rewrite them ourselves. For example, Django library we use to create web apps and there are thousands of others written by other developers. To use them we have to install them. The libraries change over time as new features are developed. When we install it will be a specific version and it may change after months when we update it our code may not work so we must change our code. By using VE we can separate the dependencies of one project from another, that way we can have different versions of the same library.

A Python virtual environment has two parts: a Python interpreter and third-party libraries installed in it. These environments are separated from each other which means any change we make in one does not affect the other. This way we can install and use different Python and library versions for different projects.

What is it used for?

To create a Python VE first we need to download and install Python on our computer. After that, we should create a folder and then create a VE inside it. We will look at different ways of creating a VE but first, let us look at a few packages we can use.

packages for handling VE

There are some tools to work with Python virtual environment. Some of these tools are:

  • Venv – which comes pre-installed with Python 3 or higher.
  • Virtualenv – is a superset of Venv which helps to create virtual environments more quickly.
  • Conda – offers packages and dependencies for Python and other languages.

How to create a Python VE with Venv Python 3?

You need to have Python installed on your computer. First using the command line create a project folder and go to that directory. Then to create a VE in the current folder, write the python version -m venv VEname and run.

Activating a Python virtual environment on window

To activate to virtual environment write the following command on cmd and run

After activating the virtual environment, its name appears in parentheses at the start of the terminal prompt as shown below:

Activating a Python virtual environment on Mac

To activate to virtual environment write the following command on cmd and run

How to deactivate

After you finish working with a virtual environment, or you want to change to another virtual environment, you can deactivate an environment by running the following command:


How to install packages on VE

Let’s see some examples of how to install third-party packages on Python VE. We can check the pre-installed packages on the virtual environment by running the pip list command.

We need to install other dependencies which are not pre-installed. Let's look at some examples:

 Let's how to install the selenium package. Selenium is a free (open source) automated testing framework used to validate web applications across different browsers and platforms.

Let's take another example of how to install packages. Let's how to install the Django package. Django is a popular Python framework for building web applications.

How to delete python VE

If you want to delete a virtual environment, you can simply delete its folder, no uninstall is required.

How to use VE on python2.0

You can use Virtualenv tool to install the Python virtual environment on Python 2.0. It is a tool used to create an isolated Python environment. You must have Python 2 and PIP installed on your system. Use pip2 to install virtualenv Python module. To install VE on Python 2 follow the following steps:

  • Get the full file path to the version of Python you just installed.
  • Go to the directory you want to create your project. This is where you’ll create the new virtual environment.
  • Create the virtual environment at the same time you specify the version of Python you wish to use. The command will be virtualenv path venvName
  • Then to activate the VE run the command: activate. 


In conclusion, the python virtual environment is a great tool to help keep our code base working while updating Python and dependencies to new versions and being able to use different versions for separate projects. 




Learn the complete Python programming language

Read more

Stay connected with news and updates!

Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.

We hate SPAM. We will never sell your information, for any reason.