我正在开发一个使用 Poetry 进行依赖管理的 Python 项目。最近,我在第三方包中发现了一个错误,因此我克隆了它的存储库以在本地进行处理。我的目标是将这个本地版本的包集成到我的项目中,但我遇到了一些问题。
首先,我尝试手动将包路径添加到我的
pyproject.toml
,如下所示:
[tool.poetry.dependencies]
buggy-one = { path = './buggy-one' }
这确实安装了软件包,但不是在可编辑模式下,这对于开发来说并不理想。
接下来,我尝试使用
pip install -e ./buggy-one
以可编辑模式安装包。不幸的是,在较新版本的 Ubuntu(如 24.04)上,这会导致以下错误:
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install python3-xyz...
错误消息建议使用
pipx
作为替代方案,但这不适合我的需求,因为 pipx
在单独的环境中安装包,而我需要在我的 Poetry 管理项目中处理它。
考虑到这些挑战,2024 年在 Ubuntu 上的 Poetry 管理项目中处理本地包的正确方法是什么?有没有办法正确链接本地包,以便我可以同时开发和测试它
如果您尝试在 Ubuntu 24.04 上使用
pip
,您将收到完整的新消息:
$ pip install gettext
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.
如果您想在诗歌下安装软件包,您必须激活当前 shell 上的环境。这可以通过以下方式实现:
poetry shell
然后你可以简单地安装你想要的任何软件包。