如何使用 setuptools 在 Python 3.6 中的库分发中包含父文件夹结构? [英] How can I include the parent folder structure on a library distribution in Python 3.6 using setuptools?

查看:61
本文介绍了如何使用 setuptools 在 Python 3.6 中的库分发中包含父文件夹结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 setuptools 分发 Python 库.我有以下目录结构:

I am using setuptools to distribute a Python library. I have the following directory structure:

  /src
    /production
      setup.py
      /prod-library
        /package1
        /package2

文件夹结构必须保持这样,因为将来src下会有多个库,需要有自己的setup.py文件.因此,在这种情况下,具有 1 个父文件夹并将 setup.py 移出到根文件夹的传统答案将不起作用.

The folder structure has to stay like this because there will be multiple libraries living under src in the future and need to have their own setup.py files. So the traditional answer of having 1 parent folder and moving out setup.py to the root folder will not work in this case.

我在库的 setup.py 中使用以下内容来导出库(正在运行)

I am using the following in the setup.py of the library to export the library (which is working)

    package_dir={'': '.'},
    packages=find_packages()

在项目 tar.gz 中它看起来像这样:

Inside the project tar.gz it looks like this:

    /prod-library
      /package1
      /package2

但是在 prod-library 包 Python 文件中,引用其他模块的导入需要按如下结构:

But inside the prod-library package Python files, imports referencing other modules need to be structured as follows:

    import src.production.prod-library.package1
    import src.production.prod-library.package2

问题:将其中一个库导入其他项目后,会出现如下错误:

The problem: After importing one of those libraries to a different project, errors are raised as follows:

ModuleNotFoundError: 没有名为src.production"的模块

由于构建仅包含在/prod-library 包中,导入代码的项目由于缺少文件夹结构 (src/production) 而失败,因为构建的发行版只有/prod-library.

Since the build only drops in the /prod-library package, the project importing the code fails due to the missing folder structure (src/production) since the built distribution only has /prod-library.

我需要做的是在发行版中包含 src/production 文件夹,因此生成的 tar.gz 文件如下所示:

What I need to do is include the src/production folder in the distribution build so the resulting tar.gz file looks like this:

   /src
     /production
       /prod-library
          /package1
          /package2

我不确定如何在构建结构中获取它们,因为它们位于 setup.py 位置之上.怎样才能做到?

I am not sure how I can get those in the build structure since they are above the setup.py location. How can that be accomplished?

如果不能,那么我愿意接受有关修复导入的建议,如果这可以成为解决方案的话.

If it can’t, then I am open to suggestions about fixing the imports if that can be a solution.

推荐答案

我找到了问题的解决方案.它与 package_dir 的配置方式有关:

I found the solution to the problem. It has to do with how the package_dir was configured:

    package_dir={'': '.'}

虽然上面的 package_dir 构建了文件并按预期包含了所有子文件夹,但 egg-info 文件的 SOURCES.txt 是不正确的,显示如下:

Although the above package_dir built the files and included all subfolders as expected, the egg-info file's SOURCES.txt was incorrect and showing as follows:

    ./prod-library/__init__.py
    ./prod-library/package1/__init__.py
    etc...

当包被导入到另一个 API 时,尝试导入 prod-libary.package1.file.py 时找不到导入

When the package was imported into another API, the imports could not be found when attempting import prod-libary.package1.file.py

如下更改package_dir后,可以正常使用库:

After changing the package_dir as follows, I was able to use the library normally:

    package_dir={'.': ''}

以上有效地删除了 SOURCES.txt 文件中的 ./前缀,这会破坏导入.现在 egg-info 的 SOURCES.txt 看起来是正确的:

The above effectively removed the ./ prefix in the SOURCES.txt file which was breaking the imports. Now the egg-info's SOURCES.txt looks correct:

    prod-library/__init__.py
    prod-library/package1/__init__.py
    etc...

这篇关于如何使用 setuptools 在 Python 3.6 中的库分发中包含父文件夹结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆