Python Virtual Environment: Difference between revisions
Line 21: | Line 21: | ||
python3 -m venv venv | python3 -m venv venv | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The command creates a virtual Python environment in the specified target directory, and installs the same Python version it is run with. The command also installs <code>pip</code>. After the virtual environment is created, it is usually a good idea to upgrade <code>pip</code>: | The command creates a virtual Python environment in the specified target directory, and installs the same Python version it is run with. By default, the Python binaries are '''linked''' from within <code>venv/bin</code> directory. This is fine if the virtual environment is accessed from the system it was created, but it lead to broken links if the virtual environment is accessed from a different system. For more details on symbolic links versus copies, see "[[Symbolic Link and Copies]]" below. The command also installs <code>pip</code>. More more details on virtual environments and <code>pip</code> see "[[Virtual Environments and pip]]" below. | ||
==Symbolic Links and Copies== | |||
==Virtual Environments and <tt>pip</tt>== | |||
After the virtual environment is created, it is usually a good idea to upgrade <code>pip</code>: | |||
<syntaxhighlight lang='bash'> | <syntaxhighlight lang='bash'> | ||
venv/bin/python3 -m pip install --upgrade pip | venv/bin/python3 -m pip install --upgrade pip |
Revision as of 18:39, 2 May 2023
External
- https://peps.python.org/pep-0405/ PEP 405 – Python Virtual Environments
Internal
Overview
A virtual environment is a mechanism to isolate a set of installed dependencies. Virtual environments can be managed with tools like virtualenv
, venv
, etc. and reside in directories called venv
or .venv
within the project's root. It is a good practice to avoid storing the content of venv
or equivalent in source control. The content is populated locally on the developers' machines.
Virtual Environment Contents
A virtual environment contains a relatively large number of files (thousands).
What exactly does it contain? Does it contain a fully independent Python interpreter copy?
Virtual Environment Creation
A virtual environment can be created manually as follows:
python3 -m venv <virtual-env-dir-name>
Example:
python3 -m venv venv
The command creates a virtual Python environment in the specified target directory, and installs the same Python version it is run with. By default, the Python binaries are linked from within venv/bin
directory. This is fine if the virtual environment is accessed from the system it was created, but it lead to broken links if the virtual environment is accessed from a different system. For more details on symbolic links versus copies, see "Symbolic Link and Copies" below. The command also installs pip
. More more details on virtual environments and pip
see "Virtual Environments and pip" below.
Symbolic Links and Copies
Virtual Environments and pip
After the virtual environment is created, it is usually a good idea to upgrade pip
:
venv/bin/python3 -m pip install --upgrade pip
The dependencies can then be installed with:
venv/bin/pip install -r requirements.txt
For more details on virtual environments and dependencies see Virtual Environments and Dependencies below.
Upgrade pip for an Already Initialized Virtual Environment
To upgrade pip
within an already initialized virtual environment:
venv/bin/python3 -m pip install --upgrade pip
Virtual Environments and Dependencies
The dependencies can then be installed with:
venv/bin/pip install -r requirements.txt
TO TEST THAT: The dependencies installed in a virtual environment are used automatically if the interpreter is ./venv/bin/python
.
Also see:
Bash Wrapper that Bootstraps a Virtual Environment
Activated Virtual Environment Shell
An "activated" virtual environment means making the virtual environment Python interpreter the default interpreter for the shell session.
To activate the virtual environment:
source .venv/bin/activate
To "deactivate", run:
deactivate
or simply exit the shell.