Create and manage virtual environments in Python

[dropcap]I[/dropcap]n this writing, I aim to present concisely how to create and manage virtual environments in Python. You can go further by getting through official documentation from the Python website.

Once a virtual environment is created, we can install any package in that environment without having doubt of clashing with bases or other ones. The installation of new packages can be performed via pip as usual. Read Installing packages using pip and virtual environments to know more about installing instructions.

Create and manage virtual environments in Python
Create and manage virtual environments in Python

What are the virtual environments in Python?

Introduction

Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.

This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.

Ways of creating virtual environments

Currently, we can use either of the following commands to create a virtual environment in Python:

venv

python3 -m venv tutorial-env

virtualenv

This is a tool which you can use to create isolated Python environments for any projects.

pip3 install virtualenv
virtualenv -p python3 <directory where you want to store packages>

Demo: steps to create and manage a virtual environment

References

  1. https://docs.python.org/3/tutorial/venv.html
  2. https://stackoverflow.com/questions/11005457/how-do-i-remove-delete-a-virtualenv
  3. https://python-guide-cn.readthedocs.io/en/latest/dev/virtualenvs.html
  4. https://towardsdatascience.com/all-you-need-to-know-about-python-virtual-environments-9b4aae690f97
  5. https://realpython.com/python-web-scraping-practical-introduction/
  6. https://sourabhbajaj.com/mac-setup/Python/virtualenv.html
  7. https://virtualenv.pypa.io/en/latest/installation.html
  8. https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe

 

Nguyen Vu Ngoc Tung

I love making new professional acquaintances. Don't hesitate to contact me via nguyenvungoctung@gmail.com if you want to talk about information technology, education, and research on complex networks analysis (i.e., metabolic networks analysis), data analysis, and applications of graph theory. Specialties: researching and proposing innovative business approaches to organizations, evaluating and consulting about usability engineering, training and employee development, web technologies, software architecture.

https://www.itersdesktop.com/author/nvntung/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.