Skip to main content
  1. Notes/
  2. Python/

Virtual Env

Table of Contents
python - This article is part of a series.
Part 2: This Article
Virtualenv allows you to setup an environment and use it without breaking your machine.

The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site directories. 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.

When used from within a virtual environment, common installation tools such as pip will install Python packages into a virtual environment without needing to be told to do so explicitly.

Setup
#

For example we’ll make a project called myprojectname:

[user@host ~]$ python3 -m venv \
                --system-site-packages \
                --prompt myprojectname \
                venv

Use
#

[user@host ~]$ source venv/bin/activate

Tip

this is recommended but not needed

(myprojectname) [user@host ~]$ pip install -U pip
Requirement already satisfied: pip in ./venv/lib/python3.9/site-packages (21.2.3)
Collecting pip
  Downloading pip-24.0-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 5.3 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.2.3
    Uninstalling pip-21.2.3:
      Successfully uninstalled pip-21.2.3
Successfully installed pip-24.0

End
#

To end

(myprojectname) [user@host ~]$ deactivate
python - This article is part of a series.
Part 2: This Article