Python venv EN: различия между версиями
Нет описания правки |
|||
Строка 1: | Строка 1: | ||
[[Python_venv_EN|en]] | [[Python_venv|ru]] | [[Python_venv_EN|en]] | [[Python_venv|ru]] | ||
== General information about virtual environments == | |||
In addition to globally installing dependencies for modules written in Python, it is also possible to use a local (virtual) environment, which will contain the entire set of modules needed and can be easily modified (updated, reinstalled, uninstalled, deleted, etc.) without conflict. | |||
A virtual environment is a special subdirectory within the module directory (usually referred to as venv) that includes all the necessary tools to run the module: interpreter <code>python</code> of the required version, service packages <code>pip</code> and <code>setuptools</code>, packages on which the module depends and other tools. To run the userside module using a virtual environment, all you need to do is call the python interpreter located in that virtual environment. This will automatically connect all packages from the virtual environment (venv subdirectory). | |||
There are several python modules for creating virtual environments. The most common are the classic '''virtualenv''' and the built-in standard module '''venv''', which appeared in Pyhon3. It should be noted, however, that in Debian (Ubuntu, etc.) the standard venv module is a separate package <code>python3-venv</code> that must be installed separately: <code>sudo apt-get install python3-venv</code>. | |||
== Create a virtual environment for a project using venv == | |||
The following example will describe the use of the standard venv module. | |||
Let's assume that the userside module for which you want to set up a virtual environment is located in the '''/opt/usm_checker''' directory. First you need to install all the necessary system dependencies of the module. For usm_checker, this is the python3-dev package, without which further dependency building is impossible. | |||
Go to the directory with the userside module and execute the command to initialise the virtual environment in the venv subdirectory. | |||
cd /opt/usm_checker | |||
python3 -m venv venv | |||
./venv/bin/pip install --upgrade pip setuptools | |||
Now we need to install the dependencies. However, we need to use the package manager from the virtual environment rather than the global package manager to install dependencies in the virtual environment. | |||
./venv/bin/pip install --upgrade -r requirements.txt | |||
Continue to follow the instructions for installing and configuring the userside module. Follow all steps with the correction that you must now use pip from a virtual environment. An example of using pip from a virtual environment was shown above. | |||
So, instead of running the module with the <code>sudo python3 usm_checker.py</code> command, you need to use the command: | |||
sudo ./venv/bin/python usm_checker.py | |||
Well, in cron you will have to write the absolute path to the interpreter and script. By analogy with the example from usm_checker documentation it will look like this: | |||
*/2 * * * * root /opt/usm_checker/venv/bin/python /opt/usm_checker/usm_checker.py > /dev/null 2>&1 | |||
== Using virtualenv == | |||
If you wish to use '''virtualenv''' instead of '''venv''', you must install it and then initialise the environment. The pip and setuptools base modules do not need to be updated, as the latest versions are installed when the virtual environment is created. All other commands are identical to '''venv'''. | |||
sudo pip3 install --update virtualenv | |||
cd /opt/usm_checker | |||
python3 -m virtualenv -p /usr/bin/python3.7 venv | |||
./venv/bin/pip install --upgrade -r requirements.txt | |||
And so on. | |||
== Additional information == | |||
Since the virtual environment corresponds to one particular version of python, there is no point in specifying explicitly the python3 or python3.7 interpreter, although this would not be an error. The virtual environment contains three symbolic links named python, python3, python3.7 that refer to the same executable. The same goes for the pip module - it also has three exactly the same executable files pip, pip3, pip3.7. |
Текущая версия от 16:20, 8 сентября 2023
General information about virtual environments
In addition to globally installing dependencies for modules written in Python, it is also possible to use a local (virtual) environment, which will contain the entire set of modules needed and can be easily modified (updated, reinstalled, uninstalled, deleted, etc.) without conflict.
A virtual environment is a special subdirectory within the module directory (usually referred to as venv) that includes all the necessary tools to run the module: interpreter python
of the required version, service packages pip
and setuptools
, packages on which the module depends and other tools. To run the userside module using a virtual environment, all you need to do is call the python interpreter located in that virtual environment. This will automatically connect all packages from the virtual environment (venv subdirectory).
There are several python modules for creating virtual environments. The most common are the classic virtualenv and the built-in standard module venv, which appeared in Pyhon3. It should be noted, however, that in Debian (Ubuntu, etc.) the standard venv module is a separate package python3-venv
that must be installed separately: sudo apt-get install python3-venv
.
Create a virtual environment for a project using venv
The following example will describe the use of the standard venv module.
Let's assume that the userside module for which you want to set up a virtual environment is located in the /opt/usm_checker directory. First you need to install all the necessary system dependencies of the module. For usm_checker, this is the python3-dev package, without which further dependency building is impossible.
Go to the directory with the userside module and execute the command to initialise the virtual environment in the venv subdirectory.
cd /opt/usm_checker python3 -m venv venv ./venv/bin/pip install --upgrade pip setuptools
Now we need to install the dependencies. However, we need to use the package manager from the virtual environment rather than the global package manager to install dependencies in the virtual environment.
./venv/bin/pip install --upgrade -r requirements.txt
Continue to follow the instructions for installing and configuring the userside module. Follow all steps with the correction that you must now use pip from a virtual environment. An example of using pip from a virtual environment was shown above.
So, instead of running the module with the sudo python3 usm_checker.py
command, you need to use the command:
sudo ./venv/bin/python usm_checker.py
Well, in cron you will have to write the absolute path to the interpreter and script. By analogy with the example from usm_checker documentation it will look like this:
*/2 * * * * root /opt/usm_checker/venv/bin/python /opt/usm_checker/usm_checker.py > /dev/null 2>&1
Using virtualenv
If you wish to use virtualenv instead of venv, you must install it and then initialise the environment. The pip and setuptools base modules do not need to be updated, as the latest versions are installed when the virtual environment is created. All other commands are identical to venv.
sudo pip3 install --update virtualenv cd /opt/usm_checker python3 -m virtualenv -p /usr/bin/python3.7 venv ./venv/bin/pip install --upgrade -r requirements.txt
And so on.
Additional information
Since the virtual environment corresponds to one particular version of python, there is no point in specifying explicitly the python3 or python3.7 interpreter, although this would not be an error. The virtual environment contains three symbolic links named python, python3, python3.7 that refer to the same executable. The same goes for the pip module - it also has three exactly the same executable files pip, pip3, pip3.7.