Concept of virtual environments

What is Virtual environments

A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available.

  • Any time you’re working on a Python project that uses external dependencies that you’re installing with pip, it’s best to first create a virtual environment.

  • a Python virtual environment is a folder structure that gives you everything you need to run a lightweight yet isolated Python environment.

Why

  • Main purpose of virtual environment is for dependency management.
  • For example If you install two different versions of the same package into your global Python environment, the second installation overwrites the first one.
  • If using virtual environments, you can use different versions of the same package.
  • Avoid system pollution. Imagine installing all third-party package in your global Python, that is too much.

How to use

  • After creation, you can activate it, and Install Packages Into It.
  • At this point, as long as you don’t close your terminal, every Python package that you’ll install will end up in this isolated environment instead of your global Python site-packages.
  • That means you can now work on your Python project without worrying about dependency conflicts.
  • Once you’re done working with this virtual environment, you can deactivate it. After executing the deactivate command, your command prompt returns to normal.

venv moudel

  • You can use Python’s venv module to create and manage separate virtual environments for your Python projects.
  • This lightweight “virtual environments” go with their own independent set of Python packages installed in their site directories.

Creating virtual environments

Creation of virtual environments is done by executing the command venv: python3 -m venv /path/to/new/virtual/environment

  • Running this command creates the target directory (creating any parent directories that don’t exist already) and places a pyvenv.cfg file in it with a home key pointing to the Python installation from which the command was run (a common name for the target directory is .venv).

How venvs work

  • When a Python interpreter is running from a virtual environment, sys.prefix and sys.exec_prefix point to the directories of the virtual environment, whereas sys.base_prefix and sys.base_exec_prefix point to those of the base Python used to create the environment.

  • It is sufficient to check sys.prefix == sys.base_prefix to determine if the current interpreter is running from a virtual environment.

  • You can source .venv/bin/activate activate virtual environment. It will put (.venv) in front of the pwd.
  • You can deactivate a virtual environment by typing deactivate in your shell

  • To assure that the scripts you want to run use the Python interpreter within your virtual environment, venv modifies the PYTHONPATH environment variable that you can access using sys.path.
  • It Changes Your Shell PATH Variable on Activation