如何在pyproject.toml中正确包含路径依赖项?

时间:2020-07-28 07:37:28

标签: python python-poetry

我有2个项目,结构如下:

/abc-lib
  / abc
    / __init__.py
    / main.py
  / pyproject.toml


/abc-web-api
  / src
    / __init__.py
    / main.py
  / pyproject.toml

我试图将abc-lib作为依赖项包含在abc-web-api中,因此有一个abc-web-api/pyproject.toml如下:

[tool.poetry]
name = "abc-web-api"
version = "0.0.1"
description = "Some description."
authors = ["Someone <someone@example.com>"]
repository = "https://github.com/someone/abc-web-api"
readme = "README.md"


[tool.poetry.scripts]
serve = "src.main:app"


[tool.poetry.dependencies]
python = "~3.6.8"
abc-lib = { path="../abc-lib" }


[tool.poetry.dev-dependencies]
pytest = "^3.10.1"
yapf = "^0.30.0"
flake8 = "^3.8.3"


[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

执行poetry install时,收到以下消息:

Package operations: 1 installs, 0 updates, 0 removals

  - Installing abc-lib (1.0.0 ../abc-lib)

[ModuleOrPackageNotFound]
No file/folder found for package abc-lib

“安装”语句中显示的版本号是正确的,因此我对[ModuleOrPackageNotFound]的含义感到困惑。

有人知道我该如何解决吗?谢谢

2 个答案:

答案 0 :(得分:1)

您的文件夹结构看起来有些奇怪。您似乎更喜欢“ src”变体。所以我建议如下:

./
├── abc-lib
│   ├── pyproject.toml
│   └── src
│       └── abc_lib
│           ├── __init__.py
│           └── main.py
└── abc-web-api
    ├── pyproject.toml
    └── src
        └── abc_web_api
            ├── __init__.py
            └── main.py

pyproject.toml中使用此abc-lib

[tool.poetry]
name = "abc-lib"
version = "0.1.0"
description = ""
authors = ["Someone <someone@example.com>"]


[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=1.0"]
build-backend = "poetry.masonry.api"

这在abc-web-api中:

[tool.poetry]
name = "abc-web-api"
version = "0.1.0"
description = ""
authors = ["Someone <someone@example.com>"]


[tool.poetry.dependencies]
python = "^3.6"
abc-lib = {path = "../abc-lib"}

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=1.0"]
build-backend = "poetry.masonry.api"

答案 1 :(得分:0)

要在项目文件夹之外安装文件夹,必须设置sys路径

import sys
sys.path.insert(1,'/path/to/directory')