【问题标题】:How to setup Python package config file using setup.cfg如何使用 setup.cfg 设置 Python 包配置文件
【发布时间】:2022-01-10 17:40:54
【问题描述】:

我正在打包一个具有目录结构的 Python 项目:

package-main (located on Desktop)
   - my_package
       - __init__.py
       - datasets
       - plot.py
       - shapefiles
           - __init__.py
           - shapefile1
                - shapefile1.shp
                - shapefiel1.dbf
                - (and 4 other file formats)
           - shapefile2
                - shapefile2.shp
                - shapefiel2.dbf
                - (and 4 other file formats)
       - utils.py
       - states.py
       - my_package.egg-info
   - LICENSE.txt
   - setup.cfg
   - README.md
   - tests
   - docs
   - pyproject.toml
   - dist
   - my_package.egg-info

我遇到了一个问题,当我将包my_package 上传到 testPyPI,然后使用以下命令将包下载到我的本地计算机时:

pip install my_package

它没有下载所有文件。

当我创建venv 来检查包文件,然后进入虚拟环境的包文件时,它只显示:

my_package
my_package-0.1.1.dist-info

当我打开my_package 目录时,我看到的只是以下结构:

my_package
   - __init__py
   - __pychache__
   - plot.py
   - shapefiles
        - __init__.py
        - __pychache__
   - states.py
   - utils.py

我已经确定这一定与我如何设置setup.cfg 文件有关(也许我没有告诉它下载所有的包文件?

我当前的setup.cfg 文件如下所示:

[options]
install_requires =
    numpy
    geopandas
    matplotlib
python_requires = >=3.6

packages = find:

我在这里阅读了documentation for setuptools,但这并没有完全让我明白。我还需要告诉包下载形状文件。

我已经看到多个关于如何使用setup.py 执行此操作的示例,但由于Python packaging tutorial 上建议我不使用此方法,因为它是动态与静态的,所以我无法找到任何示例在线了解如何执行此操作。

非常感谢任何指向资源或示例的链接。

问题:如何更改 setup.cfg 文件以确保它下载了软件包正常运行所需的所有文件?

编辑:

MANIFEST.in 文件如下所示:

include my_package/shapefiles/shapefile1/*.shp
include my_package/shapefiles/shapefile1/*.dbf

include my_package/shapefiles/shapefile2/*.shp
include my_package/shapefiles/shapefile2/*.dbf

【问题讨论】:

  • 请出示您的MANIFEST.in。 -- 一旦你构建了你的 sdist 和/或 wheel,查看它们以检查是否存在所有必需的文件。 -- 如果有帮助,也许看看this(但使用setup.cfg 而不是setup.py,就像你已经在做的那样)。
  • 所以MANIFEST.in 文件对于每个指定的文件都有正确的路径。在构建发行版时,它说它正在复制我想要包含的所有六个文件。但是,这些文件在完成后永远不会添加到分发中。这是否需要在setup.cfg 文件中的某处对MANIFEST.in 文件进行某种引用?
  • 这是矛盾的:“它说它正在复制所有六个文件”和“文件永远不会添加到分发中”。我没有跟随。您是在谈论源代码分发还是内置分发(轮子)? -- 无论如何,您是否尝试过添加include_package_data = True?否则我能想到的setup.cfg中没有办法引用MANIFEST.in
  • 这只是我运行python3 -m build时的输出,它表明它正在复制文件,但它们随后没有添加到dist文件夹中的.tar文件中在构建包之后。
  • 重新开始并使用setup.py 文件会更容易吗?

标签: python package setuptools python-wheel


【解决方案1】:

根据"Configuring setup() using setup.cfg files" 上的setuptools 文档,您可以尝试以下操作:

[options]
...
include_package_data = True
...

[options.package_data]
* = *.dbf, *.shp, *.ext1, *.ext2, *.ext3, *.ext4

【讨论】:

  • 是的,谢谢你。我没有包括include_package_data = True 行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 2014-10-14
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多