为什么"eggsecutable"寻找__main__?

3

我尝试制作一个可执行的*.egg文件。我可以使用以下方法创建它:我只需要在名为.zip.egg顶层放置一个__main__.py,Python将运行该__main__.py

我了解到还有一种更优雅的方法:

setup(
    # other arguments here...
    entry_points={
        'setuptools.installation': [
            'eggsecutable = my_package.some_module:main_func',
        ]
    }
)

https://setuptools.readthedocs.io/en/latest/setuptools.html#eggsecutable-scripts

当我用 setup.py bdist_egg 命令创建并运行 *.egg 文件时,会打印出以下信息:

C:\Python27\python.exe: can't find '__main__' module in <eggpath>

这意味着 Python 找不到入口点。

是否可能制作一个没有显式的 __main__.py 的可执行 egg 文件?

系统环境:

  • Windows 7
  • Python 2.7.9
  • setuptools 39.0.1 from c:\python27\lib\site-packages (Python 2.7))

更新:

我已经在 Linux 上尝试了 python3,但是也得到了同样的错误信息。


你在 my_package.some_module 中有 main_func 吗? - CristiFati
是的,有。我已经检查过了,通过将“egg”添加到路径中,我可以导入“my_package.some_module”并调用“main_func”。 - betontalpfa
1
我在Win 10、Python 3.6.5和setuptools 39.0.1下遇到了同样的问题。我正在使用这里展示的示例项目。不知何故,我无法自动将__main__.py放置在egg的顶层。我将__main__.py放在setup.py旁边,但它最终被移动到包中,因此其在.egg内的位置为example_pkg/__main__.py - Cryn
1个回答

4
看起来入口点文档有误,实际上你并不需要它们。
你可能需要像这样的东西: setup.py:
import setuptools

setuptools.setup(
    name="example_pkg",
    version="0.0.1",
    # all the other parameters

    # function to call on $ python my.egg
    py_modules=['example_pkg.stuff:main']
)

example_pkg/stuff.py

def main():
    print("egg test")

if __name__ == '__main__':
    main()

创建egg: setup.py bdist_egg 运行egg: python dist\example_pkg-0.0.1-py3.6.egg 输出结果: egg test 解决方案来源: https://mail.python.org/pipermail/distutils-sig/2015-June/026524.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接