Python’s package management system has two issues:

  • it doesn’t keep track of version numbers explicitly.
  • it installs packages globally.

Virtual environments address both these issues, by changing the “global” location to within your project folder (much akin to node_modules in JavaScript’s ecosystem). With this new location, we can use a requirements.txt to keep track of our dependencies and their versions (again, much like package.json in Js) and we won’t have conflicts with other apps.

Create Virtual Environment

python3 -m venv venv

Activate

source venv/bin/activate

For Fish Shell users

source venv/bin/activate.fish

Manage dependecies

Install a new package:

pip install <package-name>

Install from requirements.txt:

pip install -r requirements.txt

Update requirements.txt:

pip freeze > requirements.txt

Unfortunately, this will get you a list of all packages which including their dependencies. So requirements.txt is more akin to a package-lock.json

Deactivate

Run the deactivate function.

deactivate