Poetry引用本地项目报错:找不到包文件/文件夹的解决方案
在同根目录的Poetry项目间引用代码的解决方案
问题背景
现有目录结构如下:
├── lib │ └── stuff │ ├── __init__.py │ ├── math.py │ ├── poetry.lock │ └── pyproject.toml └── services └── a ├── __init__.py ├── main.py ├── poetry.lock └── pyproject.toml
services/a和lib/stuff均为通过poetry init生成的Poetry项目,两者的pyproject.toml内容分别为:
services/a的pyproject.toml
[tool.poetry] name = "a" version = "0.1.0" description = "" authors = ["Jack Evans <jack@evans.gb.net>"] readme = "README.md" [tool.poetry.dependencies] python = "^3.11" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"
lib/stuff的pyproject.toml
[tool.poetry] name = "stuff" version = "0.1.0" description = "" authors = ["Jack Evans <jack@evans.gb.net>"] readme = "README.md" [tool.poetry.dependencies] python = "^3.11" requests = "^2.31.0" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"
尝试在services/a目录下执行poetry add ../../lib/stuff时,出现如下报错:
• Installing stuff (0.1.0 /Users/jack/code/poetry-test/lib/stuff): Failed ChefBuildError Backend subprocess exited when trying to invoke build_wheel Traceback (most recent call last): File "/Users/jack/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/pyproject_hooks/_in_process/_in_process.py", line 353, in <module> main() File "/Users/jack/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/pyproject_hooks/_in_process/_in_process.py", line 335, in main json_out['return_val'] = hook(**hook_input['kwargs']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jack/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/pyproject_hooks/_in_process/_in_process.py", line 251, in build_wheel return _build_backend().build_wheel(wheel_directory, config_settings, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/folders/yn/4q7r_wwd16v279lryh84bgtc0000gq/T/tmpdoxiack6/.venv/lib/python3.11/site-packages/poetry/core/masonry/api.py", line 57, in build_wheel return WheelBuilder.make_in( ^^^^^^^^^^^^^^^^^^^^^ File "/var/folders/yn/4q7r_wwd16v279lryh84bgtc0000gq/T/tmpdoxiack6/.venv/lib/python3.11/site-packages/poetry/core/masonry/builders/wheel.py", line 80, in make_in wb = WheelBuilder( ^^^^^^^^^^^^^ File "/var/folders/yn/4q7r_wwd16v279lryh84bgtc0000gq/T/tmpdoxiack6/.venv/lib/python3.11/site-packages/poetry/core/masonry/builders/wheel.py", line 61, in __init__ super().__init__(poetry, executable=executable) File "/var/folders/yn/4q7r_wwd16v279lryh84bgtc0000gq/T/tmpdoxiack6/.venv/lib/python3.11/site-packages/poetry/core/masonry/builders/builder.py", line 85, in __init__ self._module = Module( ^^^^^^^ File "/var/folders/yn/4q7r_wwd16v279lryh84bgtc0000gq/T/tmpdoxiack6/.venv/lib/python3.11/site-packages/poetry/core/masonry/utils/module.py", line 69, in __init__ raise ModuleOrPackageNotFound( poetry.core.masonry.utils.module.ModuleOrPackageNotFound: No file/folder found for package stuff at ~/Library/Application Support/pypoetry/venv/lib/python3.11/site-packages/poetry/installation/chef.py:152 in _prepare 148│ 149│ error = ChefBuildError("\n\n".join(message_parts)) 150│ 151│ if error is not None: → 152│ raise error from None 153│ 154│ return path 155│ 156│ def _prepare_sdist(self, archive: Path, destination: Path | None = None) -> Path: Note: This error originates from the build backend, and is likely not a problem with poetry but with stuff (0.1.0 /Users/jack/code/poetry-test/lib/stuff) not supporting PEP 517 builds. You can verify this by running 'pip wheel --use-pep517 "stuff @ file:///Users/jack/code/poetry-test/lib/stuff"'.
需求:在两个Poetry项目共享同一根目录的情况下,实现services/a引用lib/stuff的代码。
可行解决方案
方法1:调整lib/stuff的目录结构,符合Poetry打包规范
报错的核心原因是Poetry无法识别stuff包的代码位置——Poetry默认要求项目代码放在与项目名称同名的子目录下。
修改lib/stuff的目录结构为:
lib/stuff/ ├── stuff/ # 新增与项目同名的子目录 │ ├── __init__.py │ └── math.py ├── poetry.lock └── pyproject.toml
然后回到services/a目录,重新执行:
poetry add ../../lib/stuff
方法2:手动添加Path依赖(无需调整目录)
如果不想修改目录结构,可以直接在services/a的pyproject.toml中添加路径依赖:
[tool.poetry.dependencies] python = "^3.11" stuff = { path = "../../lib/stuff", develop = true }
添加完成后执行:
poetry install
其中develop = true会创建代码软链接,修改lib/stuff的代码后无需重新安装即可生效,适合开发场景。
方法3:使用Poetry Workspaces构建Monorepo
如果后续有多个关联子项目,可以将根目录设为Poetry工作区统一管理:
- 在根目录创建
pyproject.toml:
[tool.poetry] name = "poetry-test-monorepo" version = "0.1.0" description = "" authors = ["Jack Evans <jack@evans.gb.net>"] [tool.poetry.workspaces] members = [ "lib/stuff", "services/a" ]
- 在
services/a的pyproject.toml中添加工作区依赖:
[tool.poetry.dependencies] python = "^3.11" stuff = { workspace = true }
- 在根目录执行:
poetry install
这种方式可以统一管理所有子项目的依赖,适合多项目协作场景。
内容的提问来源于stack exchange,提问作者Jack Evans
相关产品推荐
相关产品推荐

