setuptools 不包含我指定的任何子包

发布于 2025-01-09 03:26:27 字数 2560 浏览 0 评论 0原文

我正在尝试为我使用 Click 开发的 CLI 工具创建安装程序。问题是,由于某种原因,安装程序中未包含子模块,这会导致以下错误:

from modules.commands import modules
ModuleNotFoundError: No module named 'modules'

这是我的目录结构:

.
├── LICENSE
├── README.md
├── setup.py
├── src
│   ├── app_config
│   │   ├── __init__.py
│   │   └── configuration.py
│   └── commands
│       ├── __init__.py
│       ├── config
│       │   ├── __init__.py
│       │   └── commands.py
│       ├── modules
│       │   ├── __init__.py
│       │   └── commands.py
│       └── cli.py
└── tests

cli.py引用在config 和模块。当我直接运行 cli.py 时,它工作正常。但是,无论我尝试使用哪种模式让 setup.py 包含包,commands 中的子模块都不会包含在内。

这是 setup.py 的代码:

setup(
    name='cli',
    version='0.1.0',
    packages=find_packages(include=['src', 'src.*']),
    install_requires=[
        'Click'
    ],
    entry_points={
        'console_scripts': [
            'cli = src.commands.cli:cli'
        ]
    }
)

我已经检查了 find_packages 实现,但我看不出我指定的写入内容有任何问题多于。我也尝试过对所有包进行硬编码,但这也不起作用。我也尝试过 src/* ,因为它可能使用完整的文件路径,但这也不起作用。

编辑:更多故障排除

感谢这个问题,我尝试运行 python -c "from setuptools import setup, find_packages;打印(find_packages('src'))“。根据输出,它找到所有子模块:

['app_config', 'commands', 'commands.config', 'commands.modules']

但是,当我更新设置以包含相同的值时,它会失败。

setup.py:

setup(
    name='cli',
    version='0.1.0',
    packages=find_packages('src'),
    install_requires=[
        'Click'
    ],
    entry_points={
        'console_scripts': [
            'cli = src.commands.cli:cli'
        ]
    }
)

输出:


  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
...
      error: package directory 'app_config' does not exist
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.

I'm trying to create an installer for a CLI tool I'm developing with Click. The problem is that for some reason submodules aren't included in the installer, which results in the following error:

from modules.commands import modules
ModuleNotFoundError: No module named 'modules'

This is my directory structure:

.
├── LICENSE
├── README.md
├── setup.py
├── src
│   ├── app_config
│   │   ├── __init__.py
│   │   └── configuration.py
│   └── commands
│       ├── __init__.py
│       ├── config
│       │   ├── __init__.py
│       │   └── commands.py
│       ├── modules
│       │   ├── __init__.py
│       │   └── commands.py
│       └── cli.py
└── tests

cli.py references commands created in config and modules. When I'm running cli.py directly, it works fine. However, no matter what pattern I try for setup.py to include packages, the submodules in commands are not included.

This is the code for setup.py:

setup(
    name='cli',
    version='0.1.0',
    packages=find_packages(include=['src', 'src.*']),
    install_requires=[
        'Click'
    ],
    entry_points={
        'console_scripts': [
            'cli = src.commands.cli:cli'
        ]
    }
)

I've checked the find_packages-implementation, but I can't see anything wrong with the way I've specified what is written above. I've also tried hardcoding all packages, but that didn't work either. I've also tried with src/*, as maybe it uses full filepaths, but that didn't work either.

Edit: More Troubleshooting

Thanks to this question, I tried running python -c "from setuptools import setup, find_packages; print(find_packages('src'))". According to the output, it finds all the submodules:

['app_config', 'commands', 'commands.config', 'commands.modules']

However, when I update my setup to include the same value, it fails.

setup.py:

setup(
    name='cli',
    version='0.1.0',
    packages=find_packages('src'),
    install_requires=[
        'Click'
    ],
    entry_points={
        'console_scripts': [
            'cli = src.commands.cli:cli'
        ]
    }
)

Output:


  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
...
      error: package directory 'app_config' does not exist
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木落 2025-01-16 03:26:27

您可能缺少 MANIFEST.in 文件:

# https://packaging.python.org/guides/using-manifest-in/
graft src
global-exclude __pycache__
global-exclude *.py[cod]

并且由于您依赖 setuptools 作为构建和分发包的工具,因此建议采用最后的最佳实践,src 布局(案例)并将配置移动到 pyproject.tomlsetup.cfg 文件:

pyproject.toml

[build-system]
# https://setuptools.readthedocs.io/en/latest/setuptools.html#setup-cfg-only-projects
requires = [
  "setuptools >= 40.9.0",
  "wheel"
]
build-backend = "setuptools.build_meta"

setup.py (可选)

from setuptools import setup

setup()

setup.cfg

# https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html
[metadata]
name = [..]
version = [..]
description = [..]
long_description = file: README.rst
long_description_content_type = text/x-rst
author = [..]
author_email = [..]
# maintainer =
# maintainer_email =
license = [..]
license_file = LICENSE
# license_files =
url = [..]
download_url = [..]
project_urls =
    Documentation = [..]
    Issue Tracker = [..]
    Source Code = [..]
keywords = [..]
sclassifiers =
    [..]
platforms = any

[options]
python_requires = >=3.6
install_requires =
    Click
packages = find_namespace:
package_dir =
    = src
include_package_data = True
zip_safe = False

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
    cli = commands.cli:cli  # Update: change `src.commands.cli:cli` to
                            # `commands.cli:cli` as per the comment below

[options.extras_require]
    [..]

如果你是在 Windows 上并且您想要构建并发布您的包

pip install build wheel twine
py -m build -n  # don't forget '-n' flage to force using your project venv
pip install -e .  # for editable mode 

# then, if everything is ok then pulish it on pypi
py -m twine upload dist/*

you are probably missing MANIFEST.in file:

# https://packaging.python.org/guides/using-manifest-in/
graft src
global-exclude __pycache__
global-exclude *.py[cod]

and since you rely on setuptools as tool to build and distribute your package, it's recommended to adopt the last best practises, src layout (the case) and move the config to pyproject.toml and setup.cfg files:

pyproject.toml

[build-system]
# https://setuptools.readthedocs.io/en/latest/setuptools.html#setup-cfg-only-projects
requires = [
  "setuptools >= 40.9.0",
  "wheel"
]
build-backend = "setuptools.build_meta"

setup.py (optional)

from setuptools import setup

setup()

setup.cfg

# https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html
[metadata]
name = [..]
version = [..]
description = [..]
long_description = file: README.rst
long_description_content_type = text/x-rst
author = [..]
author_email = [..]
# maintainer =
# maintainer_email =
license = [..]
license_file = LICENSE
# license_files =
url = [..]
download_url = [..]
project_urls =
    Documentation = [..]
    Issue Tracker = [..]
    Source Code = [..]
keywords = [..]
sclassifiers =
    [..]
platforms = any

[options]
python_requires = >=3.6
install_requires =
    Click
packages = find_namespace:
package_dir =
    = src
include_package_data = True
zip_safe = False

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
    cli = commands.cli:cli  # Update: change `src.commands.cli:cli` to
                            # `commands.cli:cli` as per the comment below

[options.extras_require]
    [..]

if you are on windows and you want to build and publish your package

pip install build wheel twine
py -m build -n  # don't forget '-n' flage to force using your project venv
pip install -e .  # for editable mode 

# then, if everything is ok then pulish it on pypi
py -m twine upload dist/*
原来是傀儡 2025-01-16 03:26:27

我有完全相同的问题。您的问题帮助我从这里找到答案:

我将graft src添加到我的manifest.in中
我更改了(在 [options] 下的 setup.cfg 中)packages = find:
packages = find_namespace:

在添加移植之前,我非常困惑。
然后它可以在 src 的 main 下找到我的子包

I had exact same problem. Your question helped me find the answer from here:

I added graft src to my manifest.in
I changed (in setup.cfg under [options]) packages = find:
to packages = find_namespace:

Before I added graft I was super confused.
Then it could find my subpackage under my main in src

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文