Create a venv virtual environment using Python 3 in Debian 12

Original link: https://www.microcharon.top/tech/327.html

Python in the recently released Debian 12 bookworm uses Python 3.11+, which by default prompts users to install Python applications and library modules behind the venv virtual environment, instead of directly installing the relevant library modules through pip within the system (external) scope

If you still want to install the library module directly system-wide as before, you need to install python3-xyz , apt install python3-xyz , which will not be repeated here

 error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. If you wish to install a non-Debian packaged Python application, it may be easiest to use pipx install xyz, which will manage a virtual environment for you. Make sure you have pipx installed. See /usr/share/doc/python3.11/README.venv for more information.

Create a virtual environment using the venv command

For the current Debian 12 bookworm, it can be accessed via more /usr/share/doc/python3.11/README.venv
See how to use venv

 #在当前用户的主目录创建.venvs 虚拟环境目录mkdir -p ~/.venvs #用venvs 创建MHDDoS 的虚拟环境python3 -m venv ~/.venvs/Test #使用虚拟环境中的Python pip 安装相关库,如下所示~/.venvs/Test/bin/python -m pip install -r requirements.txt #查看当前虚拟环境下pip 安装的库~/.venvs/Test/bin/python -m pip list

Activate the virtual environment, making sure to point to the correct Python interpreter path

 source ~/.venvs/Test/bin/activate

Use deactivate in the virtual environment to exit the virtual environment

Actual operation

Screenshot1

Screenshot2

Screenshot3

References

How do I solve “error: externally-managed-environment” everytime I use pip3? – Stack Overflow

venv — Create a virtual environment — Python 3.11.4 documentation

This article is transferred from: https://www.microcharon.top/tech/327.html
This site is only for collection, and the copyright belongs to the original author.