setuptools.find_packages中的“where”参数是什么?

问题描述 投票:5回答:1

在python项目上工作,我试图分离源代码和单元测试;这是项目结构:

MyProject/
    MANIFEST.in
    README.md
    setup.py
    source/
        __init.py__
        my_project/
            __init.py__
            some_module.py
    test/
        __init.py__
        my_project/
            __init.py__
            test_some_module.py

这是setup.py文件:

from setuptools import setup, find_packages

setup(
    name='my_project',
    packages=find_packages(where='./source'),
    description='My project to be packaged',
    version='1.0.0',
    author='me'
    install_requires=[
        'fastnumbers~=2.0.1',
        'numpy~=1.14.1',
        'pandas~=0.22.0'
    ],
    extras_require={
        'dev': ['check-manifest'],
        'test': [
            'mock',
            'PyHamcrest',
            'pytest',
            'pytest-cov'
        ],
    }
)

然后,当我运行命令python3 setup.py sdist时,它失败并显示以下输出:

running sdist
running egg_info
writing my_project.egg-info/PKG-INFO
writing requirements to my_project.egg-info/requires.txt
writing dependency_links to my_project.egg-info/dependency_links.txt
writing top-level names to my_project.egg-info/top_level.txt
error: package directory 'my_project' does not exist

生成的top_level.txt文件看起来很好:

 my_project

但它看起来像setuptools不是从source文件夹开始找到要打包的模块。

  1. 我是否必须将setup.pyMANIFEST.in文件移动到source文件夹中?
  2. 但是,在where函数中,这个setuptools.find_packages论证是什么?
python setuptools packaging
1个回答
8
投票

您离工作解决方案只有一步之遥。加

package_dir={
    '': 'source',
},

setup()论点:

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

有关软件包重新映射的更多信息可以在Listing whole packages部分找到。

然而,看起来你通过在其中放置source__init__.py目录制作成python包。这是故意的吗?你有像这样的导入语句吗?

import source.my_project
from source.my_project.my_module import stuff

或类似的,使用source作为包名?然后要注意,一旦安装构建的包,导入将失败,因为在构建时包含源时会省略source。我看到两种方式:

  1. 要么删除source/__init__.py,请使用package_dir,如上所述,使my_project进入顶级包,省略source in imports(如果你得到任何错误,只需删除myproject-1.0.0.egg_info dir并用python setup.py egg_info重新创建它),或者
  2. 使用source作为顶级包:不要使用package_dir,在项目根目录中查找包(packages=find_packages()没有明确说明where)。
© www.soinside.com 2019 - 2024. All rights reserved.