Intermediate 8 min

Virtual environments and dependencies

A virtual environment gives a Python project its own package installation.

How it works

Picture two toolboxes with different versions of the same tool. Each project chooses its toolbox by choosing its Python interpreter. If an import succeeds in one terminal and fails in another, check which interpreter each terminal is using before trying to install the library again.

Walk through an example

One project can need a different version of a library from another. Create a virtual environment with python -m venv .venv and use that environment’s Python when installing its packages.

What to watch for

A virtual environment does not replace a declared dependency list. Record needed versions so another developer can reproduce the setup.

Check your understanding

How can both versions live on the same computer?

See the example and explanation
Worked example
Given Projects A and B need different versions of the same library.
Answer Use a separate virtual environment per project.

Each environment resolves installed libraries from its own package directory.

Open practice problem
Next lessonPositional and keyword arguments