我想在 ubuntu 中使用 python,但我需要使用默认未安装的附加软件包。有办法吗?到目前为止,这似乎非常令人沮丧,因为我不应该通过“全局”pip3 安装附加软件包,但另一方面,“venv”版本不起作用。
我需要一个“keyring”和“pycryptodome”包。我刚刚安装了 ubuntu 24.04。当我尝试:
$ pip3 install pycryptodome
我收到此错误:
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.12/README.venv for more information.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
user:~$
所以我想我不应该在“系统范围”安装软件包。所以我创建了一个 .venv 并安装了 pycryptodome 包。但是我不能在该 .venv 中使用“keyring”包:
user:~$ python3 -m venv .venv
user:~$ source .venv/bin/activate
(.venv) catchman:~$ pip3 install keyring
Collecting keyring
Using cached keyring-25.6.0-py3-none-any.whl.metadata (20 kB)
Collecting SecretStorage>=3.2 (from keyring)
Using cached SecretStorage-3.3.3-py3-none-any.whl.metadata (4.0 kB)
Collecting jeepney>=0.4.2 (from keyring)
Using cached jeepney-0.8.0-py3-none-any.whl.metadata (1.3 kB)
Collecting jaraco.classes (from keyring)
Using cached jaraco.classes-3.4.0-py3-none-any.whl.metadata (2.6 kB)
Collecting jaraco.functools (from keyring)
Using cached jaraco.functools-4.1.0-py3-none-any.whl.metadata (2.9 kB)
Collecting jaraco.context (from keyring)
Using cached jaraco.context-6.0.1-py3-none-any.whl.metadata (4.1 kB)
Collecting cryptography>=2.0 (from SecretStorage>=3.2->keyring)
Using cached cryptography-44.0.0-cp39-abi3-manylinux_2_28_x86_64.whl.metadata (5.7 kB)
Collecting more-itertools (from jaraco.classes->keyring)
Using cached more_itertools-10.6.0-py3-none-any.whl.metadata (37 kB)
Collecting cffi>=1.12 (from cryptography>=2.0->SecretStorage>=3.2->keyring)
Using cached cffi-1.17.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (1.5 kB)
Collecting pycparser (from cffi>=1.12->cryptography>=2.0->SecretStorage>=3.2->keyring)
Using cached pycparser-2.22-py3-none-any.whl.metadata (943 bytes)
Using cached keyring-25.6.0-py3-none-any.whl (39 kB)
Using cached jeepney-0.8.0-py3-none-any.whl (48 kB)
Using cached SecretStorage-3.3.3-py3-none-any.whl (15 kB)
Using cached jaraco.classes-3.4.0-py3-none-any.whl (6.8 kB)
Using cached jaraco.context-6.0.1-py3-none-any.whl (6.8 kB)
Using cached jaraco.functools-4.1.0-py3-none-any.whl (10 kB)
Using cached cryptography-44.0.0-cp39-abi3-manylinux_2_28_x86_64.whl (4.2 MB)
Using cached more_itertools-10.6.0-py3-none-any.whl (63 kB)
Using cached cffi-1.17.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (479 kB)
Using cached pycparser-2.22-py3-none-any.whl (117 kB)
Installing collected packages: pycparser, more-itertools, jeepney, jaraco.context, jaraco.functools, jaraco.classes, cffi, cryptography, SecretStorage, keyring
Successfully installed SecretStorage-3.3.3 cffi-1.17.1 cryptography-44.0.0 jaraco.classes-3.4.0 jaraco.context-6.0.1 jaraco.functools-4.1.0 jeepney-0.8.0 keyring-25.6.0 more-itertools-10.6.0 pycparser-2.22
(.venv) user:~$ python3
Python 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyring
>>> keyring.get_keyring().get_password('Chrome Keys', 'Chrome Safe Storage') == None
True
另一方面,“系统”python 可以做到这一点:
user:~$ python3
Python 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyring
>>> len(keyring.get_keyring().get_password('Chrome Keys', 'Chrome Safe Storage'))
24
因此,我似乎需要使用 .venv,但在该虚拟环境中,包 (keyring) 不起作用,我应该使用系统 python 包。但当我使用这些包时,我无法安装任何其他包。这有点儿像第 22 条军规
Ubuntu 是否支持一些可用的 Python 开发?或者我应该如何实现它?
这是我的系统信息(如果您需要更多信息,请告诉我):
user:~$ cat /etc/issue
Ubuntu 24.04.1 LTS \n \l
user:~$ uname -a
Linux ryzen 6.8.0-51-generic #52-Ubuntu SMP PREEMPT_DYNAMIC Thu Dec 5 13:09:44 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
user:~$ plasmashell --version
plasmashell 5.27.11
user:~$ kf5-config --version
Qt: 5.15.13
KDE Frameworks: 5.115.0
kf5-config: 1.0
user:~$ python3 --version
Python 3.12.3
user:~$ pip3 --version
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)