1.9 KiB
venv
Why must venv?
Modern Debian & Ubuntu follow PEP 668. This is why you see errors like error: externally-managed-environment. It literally means "This Python environment is managed by the OS. Don’t touch it with pip.".
venv is a module in Python that provides support for creating lightweight, isolated Python environments, known as virtual environments. Each virtual environment has its own installation directories and can have its own versions of Python packages, independent of the system-wide Python environment.
When deploying a Python application, using a virtual environment ensures that only the required packages (and their specific versions) are bundled. This reduces the risk of deploying unnecessary packages or incompatible versions that could lead to runtime errors.
Using venv is a widely accepted best practice in the Python community. It encourages good habits in dependency management, ensuring that projects are self-contained and reducing the potential for "dependency hell."
When a project is no longer needed, deleting its virtual environment is straightforward and does not affect other projects or the system's Python environment.
How it works?
When you run python3 -m venv your_venv_directory_name, Python creates a directory for “A mini Python install just for this project”. When you activate it with source .venv/bin/activate, your shell is told "When I type python or pip, use the ones inside .venv", Nothing else changes. So, when you type pip install requests, it installs only inside .venv. Then, you can deactivate venv with deactivate command.
Why I add venv on my gitignore?
Committing venv to Git is gross. Virtual environments can contain thousands of files and their size can be in gigabytes. Committing them to Git can overload and clutter your source code repo with unnecessary files and cause confusion for anyone trying to clone and run the source code on their machine.