setuptools 不包含我指定的任何子包
我正在尝试为我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能缺少
MANIFEST.in
文件:并且由于您依赖
setuptools
作为构建和分发包的工具,因此建议采用最后的最佳实践,src
布局(案例)并将配置移动到pyproject.toml
和setup.cfg
文件:pyproject.toml
setup.py (可选)
setup.cfg
如果你是在 Windows 上并且您想要构建并发布您的包
you are probably missing
MANIFEST.in
file: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 topyproject.toml
andsetup.cfg
files:pyproject.toml
setup.py (optional)
setup.cfg
if you are on windows and you want to build and publish your package
我有完全相同的问题。您的问题帮助我从这里找到答案:
我将
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.inI 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