Skip to content

virtual envs

ninabarzh edited this page Jan 24, 2022 · 1 revision

Virtual environments

Use any you like. venv+pip come pre-installed with most versions of Python, are the most popular tools for managing virtual environments and packages, and are simple to use. Alternatives that are better at figuring out dependencies are pipenv and poetry.

venv

Installation

$ sudo apt install python3-venv

Usage:

In the directory of a repository:

$ python3 -m venv ./venv

Activate:

$ source venv/bin/activate

Deactivate with deactivate.

pip

Install pip-tools with venv activated:

(venv) $ python -m pip install pip-tools
(venv) $ pip --version
(venv) $ python -m pip install --upgrade pip
(venv) $ pip --version

Usage:

(venv) $ pip list

If there is a requirements.txt (frozen requirements) install from that file (IF that works).

(venv) $ pip install -r requirements.txt

To freshly install all that is needed (for an existing repo with existing dependencies, this can break things), install pipreqs:

(venv) $ pip install pipreqs

Run pipreqs in the project root folder, and pipreqs generates a requirements.txt file for any project based on imports. Then install from the created requirements.txt with pip:

(venv) $ pip install -r requirements.txt

pip freeze saves the packages that are installed with pip install:

(venv) $ python -m pip freeze > requirements.txt
Clone this wiki locally