Home Python How to Create Virtual Environments in Python

How to Create Virtual Environments in Python

In this tutorial, we will learn how to create virtual environments in Python. We will first look at what is a virtual environment and why we need it and then see how to can we create it and use it. We will be using the virtualenv library for creating virtual environments.

Introduction

As the name suggests, Virtual Environments are tools used for separating different python environments on the same computer. Primarily a system can have only a single python installation with all its libraries. But the Virtual Environments provides a separate virtual isolated system in which we can install different libraries and frameworks of python and used those packages for a project instead of using the packages installed in the main installation of python. This method is useful when a specific version of a package is needed for a project.

We can create virtual environments by using many tools like venv, anaconda, virtualenv, and pipenv, but we were using the virtualenv library to create virtual environments for this tutorial.

Why Virtual Environments

Python is a great programming language with many libraries and frameworks like pandas, flask Django, etc. The libraries and frameworks in python keep updating, making it bug-free, and new and powerful features come with those new releases. Updating a python package is a great thing to do, but we may also have some problems after updating a package.

For example, assume you have projects built of the Django framework of version 2.2. If you upgrade your system to the Django version above 2.2 and run the project, you could end up with errors on running the code. To deal with this problem, you can use virtual environments. We can create virtual environments and install packages to be used for that project. So if we need a specific version of a python package for our code, we can create a virtual environment, install the package there, and then use those packages in our code.

The virtualenv library

A library named virtualenv can be used to create virtual environments. It does not come pre-installed in python, and so needs to be done manually using python’s pip package manager. Run the following command in the terminal.

pip install -U virtualenv

After installing the virualenv library, check the installation by typing the following command in the terminal.

virtualenv --version

This will display the version of the virtualenv library as shown in the below image.

checking the version of virtualenv tool
checking the version of virtualenv tool

Creating a Virtual Environment

To create a virtual environment using the virtualenv tool and having the name myvenv, type the following command in the terminal.

virtualenv myvenv

On running the above code we will have a new folder created in the current working directory with the name myvenv. This is the folder for the virtual environment. In the myenv directory, there should be two subdirectories and a file. The bin subdirectory contains the binaries of python, pip, etc and the lib directory contains the python libraries. There is also a file present in the myvenv directory having the name pyvenv.cfg that contains some configuration of the virtual environment.

Output:

creating a virtual environment in python
creating a virtual environment in python

Activating a Virtual Environment

Virtual environment should be first activated in order to install packages in it and use them. To do that, run the activate script present in the bin directory. On Linux, run the below shown syntax in the terminal to activate the virtual environment.

source virtual_environment_name/bin/activate

To activate a virtual environment in windows, type the command following the below syntax in the command prompt or powershell.

virtual_environment_name/scripts/activate 

For example, to activate the virtual environment myvenv that we created previously, type the following command in the terminal.

source myvenv/bin/activate

Output:

activating a virtual environment in python
activating a virtual environment in python

In the above image, we can observe the symbol (myvenv) appeared at the beginning of the second line which means that the virtual environment myvenv has been activated.

Installing Packages in a Virtual Environment

The main motive of creating a virtual environment is to install and use a specific version of the Python package without installing it in the default python installation. To install a package in the virtual environment, activate the virtual environment by using the above method. Then use any package manager like pip to install the python packages on it. For example, we can install the latest Django version by typing the following command in the terminal.

pip install -U django

On running the above command, the latest version of Django should be installed in the virtual environment. Then use this package by importing the package in the python code and running the python code while the virtual environment is activated.

Creating a requirements.txt file

When a code is shared with other people, they need to create a virtual environment and install all the libraries used by the original code creator. For a short project, installing a few libraries is not a big deal. But for a large project, it isn’t easy to install each of the libraries one by one.

To deal with this problem, the pip package manager can list all the new packages installed in the virtual environment and write them into a file. It’s the requirements.txt file. Run the following command in the terminal to create one.

pip freeze > requirements.txt

Output:

creating a requirements.txt file
creating a requirements.txt file

After creating the requirements.txt, use the pip package manager to install those libraries in some other environment. To see a practical example, just create a new virtual environment, activate it, and then move to the directory containing the requirements.txt file and run the following command.

pip install -r requirements.txt

On running the above code all the packages present in the requirements.txt file will be installed in the new virtual environment.

Output:

installing the packages of a requirements.txt file
installing the packages of a requirements.txt file

Deactivating the virtual environment

After done with the virtual environment you can deactivate it and return to the original environment. Use the following command in the terminal and hit enter.

deactivate

On running the above command, we will get out of the virtual environment, and the (myvenv) symbol will be disappeared. Look at the below image.

deactivating a virtual environment
deactivating a virtual environment

Conclusion

That’s all about how to create and use a virtual environment in Python. We have also seen how to create a requirements.txt file using the pip package manager, making it easy to install python packages of a virtual environment into another virtual environment. You may also want to see our guide on working with pdf files in python, which discusses how to perform various operations with PDF files using python.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More