This note explains how to create isolated Python environment and install
Python packages. In these notes used mostly Python Virtual Environment
tutorial and FastAPItutorial.
The venvPython 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.
— venv
Virtual environment is a tool to create isolated Python and packages
environment for your program (projects, services, etc.).
Python applications often using packages and modules that don’t come as part
of the standard library (dependencies).
Applications sometimes need a specific version of a library, because the
application may require that a particular bug has been fixed, or the application
may be written using an obsolete version of the library’s interface
(compatibility and testing). This behavior creating conflicts, each application
sometimes requiring own version of a library, and using global packages isn’t
working.
What if we just use only new version of packages in each project?
It’s very common in Python packages to try the best to avoid breaking changes in
new versions, but it’s better to be safe, and install newer versions
intentionally and when you can run the tests to check
everything is working correctly.
Also, depending on your operating system (e.g. Linux, Windows, macOS), it could
have come with Python already installed. And in that case it probably had some
packages pre-installed with some specific versions needed by your system. If
you install packages in the global Python environment, you could end up breaking
some of the programs that came with your operating system.
The solution for this problem is to create a virtual environment, a
self-contained directory tree (usually .venv) that contains a Python
installation for a particular version of Python, plus a number of additional
packages. Each application can use own virtual environment.
Which standard python module used to create virtual environments?
The module used to create and manage virtual environments is called venv.
Which Python version will be installed in the virtual environment when you use
venv module?
venv will install the Python version from which the command was run (as
reported by the --version option). For instance, executing the command with
python3.12 will install Python 3.12.
Using “venv” module
Create a standard Python virtual environment and install packages:
To install (upgrade, remove) packages you would normally use the pip command
that comes with Python (built-in).
By default, pip will install packages from the Python package
index into your global Python environment (the global
installation of Python), which brings some package version
problems.