SoFunction
Updated on 2025-05-13

Python activates the implementation of virtual environment (venv)

When activate Python's virtual environment (venv) When , the activation script will make a series of temporary modifications to the system environment, allowing the terminal's Python and related tools to use the versions and dependencies in the virtual environment. These operations will not permanently change the system environment and will only take effect in the current terminal session.

Detailed explanation of the operation of activate the virtual environment

1. Modify PATH environment variables

Activating the script will convert the virtual environmentbin(Linux/MacOS) orScripts(Windows) directory added to environment variablesPATHThe beginning of  .

In this way, thepythonandpipTools such as  will be found and executed first, rather than the global version of the system.

ModifiedPATHMay be similar to:

PATH=<venv_path>/bin:$PATH  # Linux/MacOS
PATH=<venv_path>\Scripts;%PATH%  # Windows

For example, runpythonWhen  , what is actually executed is in the virtual environmentpythonBinary file.

2. Set VIRTUAL_ENV environment variable

The activation script will set up aVIRTUAL_ENVEnvironment variables point to the root directory of the virtual environment, for example:

VIRTUAL_ENV=/path/to/venv

This variable can be used by some tools (such as IDE or scripts) to detect whether it is currently in a virtual environment.

3. Modify the terminal prompt (Prompt)

Activating the script will modify the terminal's prompt (for examplePS1), add the name of the virtual environment in front of the original prompt to remind the user that he is currently in the virtual environment:

(venv) user@hostname:~/project$

This is by adjusting Shell environment variables (e.g.PS1) implemented.

4. Use python and pip in virtual environments

After activation,pythonandpipCommands will point to executable files in the virtual environment. For example:

which python  # Linux/MacOS
# Output: /path/to/venv/bin/python
where python  # Windows
# Output: C:\path\to\venv\Scripts\

The installed Python library will be placed in the virtual environment.site-packagesIn the directory, not the system global directory.

5. Dependencies for loading virtual environments

  • Python in the virtual environment comes with an isolated one, this path only contains thesite-packagesTable of contents.
  • After activating the virtual environment, all imported libraries (import) will only depend on virtual environments.

Activate script implementation

  • Linux/MacOS
    The activation script is a shell script with the path<venv>/bin/activate
  • Windows
    Windows provides multiple activation scripts:
    • <venv>\Scripts\(Applicable to CMD).
    • <venv>\Scripts\Activate.ps1(Applicable to PowerShell).

The main logic of these scripts is to modifyPATHand other environment variables.

Summarize

The core of activating a virtual environment is to switch to an isolated Python environment by adjusting the terminal's environment variables, thereby using dependencies and tools in the virtual environment without affecting the global system environment.

This is the end of this article about python activation virtual environment (venv). For more related python activation virtual environment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!