发布的PyPi包(带Poetry)安装后丢失文件

问题描述 投票:0回答:2

我有以下项目:

| my-projectttttttt
│
├───dist
│       my_projectttttttt-1.0.0-py3-none-any.whl
│       my_projectttttttt-1.0.0.tar.gz
│
├───src
│   ├───my_projectttttttt
│   │       __init__.py
│   │       __main__.py
│   │
│   └───resources
│           my_projectttttttt.png
│
├───tests
│       test_my_project.py
|
│ LICENSE.md
│ pyproject.toml
│ README.md

我正在尝试使用此 pyproject.toml 文件通过 PyPi 上的 Poetry 发布它:

[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "my-projectttttttt"
version = "1.0.0"
description = "The description of the package"
license = "MIT"
authors = ["George K. Wright"]
repository = "https://github.com/"
homepage = "https://github.com/"

packages = [
    { include = "my_projectttttttt", from = "src"},
]
include = [
    { path = "src/resources" },
]


[tool.poetry.dependencies]
python = ">=3.6"
requests="^2.28.1"


[tool.poetry.scripts]
my-projectttttttt = "my_projectttttttt.__main__:main"

当我使用

poetry build
生成的存档构建项目时:

C:\my-projectttttttt\dist\my_projectttttttt-1.0.0.tar.gz\my_projectttttttt-1.0.0.tar\my_projectttttttt-1.0.0\src\

同时具有

my_projectttttttt
模块和
resources
文件夹,其中包含 png,但是当我使用
poetry publish
发布它,然后使用
pip install my-projectttttttt
通过 pip 安装它时,安装文件夹:

AppData\Local\Programs\Python\Python39\Lib\site-packages\my_projectttttttt

仅包含

__init__.py
文件和
__main__.py
文件。

那么我的 png 资源文件夹在哪里?我究竟做错了什么?我尝试了很多不同的东西(有或没有

src
结构)但没有运气,这里类似的帖子根本没有帮助我。

非常感谢。

python python-packaging python-poetry
2个回答
1
投票

您的项目目录结构使您的项目有 2 个顶级可导入包:

my_projectttttttt
resources
。 PNG 文件实际上是
resources
顶级可导入包的一部分,而不是
my_projectttttttt
顶级可导入包的一部分。当您以这种形式pip安装此项目时,您应该有一个
AppData\Local\Programs\Python\Python39\Lib\site-packages\resources
目录。

如果我是你,我宁愿让

resources
包成为
my_projectttttttt
的子包,并且可以作为
my_projectttttttt.resources
导入。为此,您需要将
resources
移动为
my_projectttttttt
的子目录:
my_projectttttttt/resources
。这会让很多事情变得更容易。您可能还需要在
pyproject.toml
文件中进行一些小调整,很可能不再需要
include
packages
数组。


0
投票

您在

include
中的
pyproject.toml
陈述不正确。该路径应具有
/*
后缀,即:

include = [
    { path = "src/resources/*" },
]
© www.soinside.com 2019 - 2024. All rights reserved.