简体   繁体   English

Python package 未安装子模块

[英]Python package not installing submodules

I have created a package with the following structure in the dev branch (not merging to main until I verify the package installs correctly):我在 dev 分支中创建了一个具有以下结构的 package(在我验证 package 安装正确之前不合并到 main):

mypackage
|
|-- __init__.py
|-- setup.py
|-- requirements.txt
|-- module.py
|-- subpackage_one
    |
    |-- __init__.py
    |-- module_ab.py
        |-- class_aba
        |-- class_abb
    |-- module_ac.py
        |-- function_aca

|-- subpackage_two
    |
    |-- __init__.py
    |-- module_ba.py
        |-- function_baa

Additional information:附加信息:

  • The __init__.py files at root and in subpackage__two are both empty root 和subpackage__two中的__init__.py文件都是空的
  • The __init__.py file in subpackage_one contains some additional initialization in the form of from mypackage.subpackage_one.module_xx import class_xxx (or function_xxx ) subpackage_one中的__init__.py文件包含一些额外的初始化,形式为from mypackage.subpackage_one.module_xx import class_xxx (或function_xxx
  • I am installing the package via pip install git+https://github.com/organization/repo.git@dev我正在通过pip install git+https://github.com/organization/repo.git@dev package
  • If I am in the root directory of the package, I can import the submodules as expected如果我在 package 的根目录下,我可以按预期导入子模块
  • The setup.py file is: setup.py 文件是:
import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name='mypackage',
    version='0.0.2',
    author='author1, author2',
    author_email='author1_email, author2_email',
    description='My Package',
    long_description=long_description,
    long_description_content_type="text/markdown",
    url='https://github.com/organization/repo',
    packages=['mypackage'],
    install_requires=['requests'],
)
  • When I run the following snippet:当我运行以下代码段时:
import pkgutil
for i in pkgutil.iter_modules(mypackage.__path__):
    print(i)

I see:我懂了:

ModuleInfo(module_finder=FileFinder('/path/to/package/mypackage'), name='module', ispkg=False)

And indeed, the subpackages are not in the mypackage folder.事实上,子包不在 mypackage 文件夹中。

How can I get the subpackages to install along with the package?如何让子包与 package 一起安装?

Your issue might be the packages parameter.您的问题可能是packages参数。 It needs to be supplied with every module or 'package'.它需要随每个模块或“包”一起提供。

setuptools has a nice function to find them, use it like this: packages=setuptools.find_namespace_packages(), setuptools有一个不错的 function 可以找到它们,像这样使用它: packages=setuptools.find_namespace_packages(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM