[英]Setup.py install. Package init file 'graph\__init__.py' not found (or not a regular file)
[英]How do I force setup.py to include the `__init__.py` file and make my package importable?
我正在尝试让使用pip
安装它的人可以导入我的 Python项目。 虽然它包含一个__init__.py
并在本地作为 package 工作,但似乎我误解了 setuptools 的工作原理。
我运行以下三个命令来上传 package。
python3 setup.py sdist bdist_wheel
python3 -m pip install --upgrade twine
python3 -m twine upload dist/*
然后在另一台机器上,我运行pip3 install smux.py
。 结果是我可以将smux.py
作为命令访问,但在尝试导入时出现导入错误。
python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import smux
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'smux'
运行上面的setup.py
命令后,我检查了当前目录中的smux.py.egg-info/SOURCES.txt
并发现它包含以下内容:
README.md
setup.py
smux.py
smux.py.egg-info/PKG-INFO
smux.py.egg-info/SOURCES.txt
smux.py.egg-info/dependency_links.txt
smux.py.egg-info/top_level.txt
文件__init__.py
丢失。
如何将该文件放入 package,或修改设置调用以使smux
可导入?
您的setup
调用如下所示:
setup(
name="smux.py",
version='0.1.1',
scripts=['smux.py'],
author="Henry Qin",
author_email="root@hq6.me",
description="Simple tmux launcher that will take less than 2 minutes to learn and should work across all versions of tmux",
long_description=long_description,
platforms=["All platforms that tmux runs on."],
license="MIT",
url="https://github.com/hq6/smux"
)
通过scripts
参数,您已经告诉 setuptools smux.py
是一个脚本,而不是一个模块。 因此,它被安装为脚本,而不是模块。
您的代码不需要__init__.py
也不应该有。 拥有__init__.py
将无济于事。 您需要告诉 setuptools 将文件安装为模块而不是脚本,并单独注册一个入口点,并在entry_points
参数中使用console_scripts
条目:
setup(
...
# no scripts= line
py_modules=['smux'],
entry_points={
'console_scripts': ['smux=smux:main']
}
)
然后,当您的代码安装后, smux
将是可导入的,并且将有一个可从命令行调用smux.main
的smux
脚本。
另一个答案是更犹太和现代的答案,如果我没有旧脚本,我会使用它并将脚本命名为smux
,具体取决于名称smux.py
。 不幸的是,其他人已经包装smux.py
,破坏他们的脚本是不友好的。
这是我为避免因遗留脚本名称以.py
结尾并与模块命名冲突而引起的循环依赖问题而采取的措施。
我最终修改了setup
调用以具有以下两个。
py_modules=['smux'],
scripts=['smux.py']
我的实验表明,这具有使模块可导入的预期效果,同时保留名为smux.py
的工作脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.