Search code examples
pythonpippython-3.6entry-point

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


Given these 2 files:

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...')

And after doing pip -e ., the next files will be installed into my python virtual env 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

The problem comes when i try to run mcve.exe, I'll get the next traceback:

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__'

Also, not sure whether is worth mentioning but python I got my python files to be opened with by St, ie:

>assoc .py
.py=Python.File

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

I guess that's not the problem... so... it must be something wrong in my setup.py, how can i fix this error? I've tried to put all the content in a package and reference it that way but no luck either :/

Thanks in advance.


Solution

  • If you want to use entry_points, you need to provide a function. You specified a function called __main__ but didn't provide it.

    You should choose a better function name, and do your logic there instead of top-level if __name__ == "__main__":, if you want to use entry points.