ImportError: module 'main' has no attribute '__main__' after doing pip -e

ImportError: module 'main' has no attribute '__main__' after doing pip -e

给定这 2 个文件:

setup.py:

from setuptools import setup, find_packages

setup(name='mcve',
      version='0.0.1',
      description='',
      long_description=(''),
      author='BPL',
      author_email='',
      entry_points={
          'console_scripts': ['mcve = main:__main__']
      },
      packages=[],
      license='MIT')

main.py:

if __name__ == "__main__":
    print('running the mcve...')

并且在执行 pip -e . 之后,下一个文件将安装到我的 python 虚拟环境 3.6.2/win7:

d:\virtual_envs\py362_32\lib\site-packages\mcve.egg-link
d:\virtual_envs\py362_32\scripts\mcve-script.py
d:\virtual_envs\py362_32\scripts\mcve.exe
d:\virtual_envs\py362_32\scripts\mcve.exe.manifest

当我尝试 运行 mcve.exe 时,问题来了,我会得到下一个回溯:

Traceback (most recent call last):
  File "d:\virtual_envs\py362_32\lib\site-packages\pkg_resources\__init__.py", line 2413, in resolve
    return functools.reduce(getattr, self.attrs, module)
AttributeError: module 'main' has no attribute '__main__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\virtual_envs\py362_32\Scripts\mcve-script.py", line 11, in <module>
    load_entry_point('mcve', 'console_scripts', 'mcve')()
  File "d:\virtual_envs\py362_32\lib\site-packages\pkg_resources\__init__.py", line 570, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "d:\virtual_envs\py362_32\lib\site-packages\pkg_resources\__init__.py", line 2751, in load_entry_point
    return ep.load()
  File "d:\virtual_envs\py362_32\lib\site-packages\pkg_resources\__init__.py", line 2405, in load
    return self.resolve()
  File "d:\virtual_envs\py362_32\lib\site-packages\pkg_resources\__init__.py", line 2415, in resolve
    raise ImportError(str(exc))
ImportError: module 'main' has no attribute '__main__'

此外,不确定是否值得一提,但 python 我的 python 文件由 St 打开,即:

>assoc .py
.py=Python.File

>ftype Python.File
Python.File="d:\software\SublimeText3_x64\sublime_text.exe %1" %*

我想这不是问题所在...所以...我的 setup.py 一定有问题,我该如何解决这个错误?我试图将所有内容放在一个包中并以这种方式引用它,但也没有运气:/

提前致谢。

如果要使用entry_points,需要提供一个函数。您指定了一个名为 __main__ 的函数,但没有提供它。

如果你想使用入口点,你应该选择一个更好的函数名称,并在那里而不是顶层执行你的逻辑 if __name__ == "__main__":