How to create a virtual environment in Python?

LearnTips LearnTips How to create a virtual environment in Python?

Tagged: 

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #4141
    Santhosh Kumar D
    Keymaster
    @santhosh

    The venv module allows us to create a virtual environment within a site’s directory. Each virtual environment has its own Python library and can have its own independent set of installed Python packages. In this tutorial, I am going to use Python 3.8.2 and Command Prompt to create a virtual environment.

    Create a virtual environment

    First, we have to move to the directory within which we want to create a virtual environment. Use the command venv to create a virtual environment. For instance, let’s create a directory called environment, and create a virtual environment inside that directory as shown below.

    python -m venv environment

    Alternatively, we can directly mention the path of the directory within which we want to create a virtual environment there. For instance, the below command will create a directory called environment and creates a virtual environment inside the directory as per the mentioned path.

    python -m venv /directory/site/environment

    Activate a virtual environment

    Once a virtual environment has been created, we need to activate it using a platform-specific script in the virtual environment’s directory. The following commands can be used to activate a virtual environment.

    PlatformShellCommand to activate virtual environment
    WindowsCommand Promptvenv\Scripts\activate.bat
    PowerShellvenv\Scripts\Activate.ps1
    POSIXbash/zshvenv/bin/activate
    fishvenv/bin/activate.fish
    csh/tcshvenv/bin/activate.csh
    PowerShell Corevenv/bin/Activate.ps1

    Note: Replace venv with the path of the directory containing the virtual environment.

    For instance, in this tutorial, we have created a virtual environment inside a directory called environment. To activate it, we can use the Command Prompt specific activation script as follows.

    environment\Scripts\activate.bat

    Our virtual environment is now active as it shows the name (environment) in the command line.
    How to create a virtual environment in Python?

    Source: venv — Creation of virtual environments

    Learn more Python tips.

Viewing 1 post (of 1 total)