Table of Contents

    The virtual environment venv module (Lib/venv/) supports creating "virtual environments", independent packages installed in a separate directory. A virtual environment is created on top of an existing Python installation (Virtual Environments base).

    Why Virtual Environment:

    A Python virtual environment is very important. It prevents package version or Dependency conflicts between the projects.

    Suppose there are 2 projects: Project A and Project B. Project A needs version 1 package, and Project B needs version 2 package. If projects A and B are running without a virtual environment, there may be a possibility of conflict between the 2 versions of the project. It allows testing with different versions of projects

    How to install a Python virtual environment

    Python has a built-in venv module to install Python Packages

            python -m venv firstProject

            or

            python3 -m venv firstProject

    The first Project folder structure will look like this:

            bin  include  lib  lib64  pyvenv.cfg

    How to activate a Python virtual environment

    To use the virtual environment created, you have to activate it. To activate the virtual environment created, type the following command,

        source firstProject/bin/activate

    If the virtual environment is activated, you will get a response like this,

        (myfirstproject) izzu@izzumon:~/Documents/fastapi_interview/myfirstproject$

    Install Packages using PIP

    If Python virtual environment is activated, you can install the packages using PIP using the following command,

        pip install

    To List installed packages inside a Virtual Environment

          pip list

    How to deactivate a Python virtual environment?

    Use the below command to deactivate a Python virtual environment:

         deactivate

    When the Python virtual environment is deactivated, installed packages inside the environment cannot be used until activated. But you can reactivate it and use it.

    To reactivate the Python virtual environment, use the same command that was used to activate the environment.