构建一个具有非标准目录结构的软件包。

huangapple go评论67阅读模式
英文:

Poetry: build a package with a non-standard directory structure

问题

I created a repo with this non-standard structure:

- src
 - resources
  -- a.py
  -- b.py ...

I want to make a package out of it (let's call it pack).

In my pyproject.toml, the relevant lines are:

[tool.poetry]
name = "pack"
include=[{include="src"}]

Then after a pip install everything is installed under src. Using it would mean from src import ... not from pack import ....

If I follow the standard tree (everything under src/pack), then it works as expected.

Question

Is there a way, with my tree, to have the package built so as from pack import ... works?

英文:

I created a repo with this non-standard structure:

- src
 - resources
  -- a.py
  -- b.py ...

I want to make a package out of it (let's call it pack).

In my pyproject.toml, the relevant lines are:

[tool.poetry]
name = "pack"
include=[{include="src"}]

Then after a pip install everything is installed under src. Using it would mean from src import ... not from pack import ....

If I follow the standard tree (everything under src/pack), then it works as expected.

Question

Is there a way, with my tree, to have the package built so as from pack import ... works?

答案1

得分: 1

如果你想在你的import语句中使用pack,你需要一个叫做这样的文件夹。这是Python发现一个包的方式。

它的位置以及你的项目如何命名是另一回事。如果你的项目结构像这样:

pack
├── README.md
├── pyproject.toml
└── src
    └── pack
        ├── __init__.py
        ├── a.py
        └── b.py

你必须在你的pyproject.toml中有这样的配置:

[tool.poetry]
name = "pack"
packages = [{include = "pack", from = "src"}]
英文:

If you want to use pack in your import statements, you need a folder that is called like this. This is how python discovers a package.

Where it is located and how your project is named is a different story. If you have a project structure like this:

pack
├── README.md
├── pyproject.toml
└── src
    └── pack
        ├── __init__.py
        ├── a.py
        └── b.py

you must have this in your pyproject.toml

[tool.poetry]
name = "pack"
packages = [{include = "pack", from = "src"}]

huangapple
  • 本文由 发表于 2023年5月22日 20:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定