"Объект AttributeError: 'module' не имеет атрибута" с установленным пакетом

У меня есть проект с этим содержанием:

proj ├── src │ ├── scriptA.py │ ├── scriptB.py │ └── __init__.py ├── LICENCE ├── README.md └── setup.py 

Я следую этому руководству, чтобы упаковать этот проект для PiPY. Файл setup.py выглядит так:

 #!/usr/bin/python3 # coding=utf8 from setuptools import setup setup( name = "proj", version = "0.2", packages = ['src'], install_requires=[], entry_points={ 'console_scripts': [ 'scriptA=src:scriptA', 'scriptB=src:scriptB' ], }, # metadata for upload to PyPI author = "Luís", author_email = "[email protected]", description = "Some package", license = "EUPL v1.1", keywords = "pip package", url = "https://some.place.com", # project home page, if any classifiers = [ "Programming Language :: Python", "Programming Language :: Python :: 3", "Development Status :: 4 - Beta", "Environment :: Console", "Intended Audience :: Science/Research", "License :: OSI Approved :: European Union Public Licence 1.1 (EUPL 1.1)", "Operating System :: OS Independent", "Topic :: Scientific/Engineering :: GIS" ], # could also include long_description, download_url, classifiers, etc. ) 

Файл __init__.py содержит следующее:

 __all__ = ["scriptA", "scriptB"] 

Сценарии просто печатают текстовые сообщения. Это scriptA.py :

 def main(): print ("This is scriptA!") main() 

Пакеты строятся успешно:

 $ python3 setup.py bdist_wheel --universal running bdist_wheel running build running build_py creating build creating build/lib creating build/lib/src copying src/scriptA.py -> build/lib/src copying src/__init__.py -> build/lib/src copying src/scriptB.py -> build/lib/src installing to build/bdist.linux-x86_64/wheel running install running install_lib creating build/bdist.linux-x86_64 creating build/bdist.linux-x86_64/wheel creating build/bdist.linux-x86_64/wheel/src copying build/lib/src/scriptA.py -> build/bdist.linux-x86_64/wheel/src copying build/lib/src/__init__.py -> build/bdist.linux-x86_64/wheel/src copying build/lib/src/scriptB.py -> build/bdist.linux-x86_64/wheel/src running install_egg_info running egg_info creating proj.egg-info writing proj.egg-info/PKG-INFO writing top-level names to proj.egg-info/top_level.txt writing entry points to proj.egg-info/entry_points.txt writing dependency_links to proj.egg-info/dependency_links.txt writing manifest file 'proj.egg-info/SOURCES.txt' reading manifest file 'proj.egg-info/SOURCES.txt' writing manifest file 'proj.egg-info/SOURCES.txt' Copying proj.egg-info to build/bdist.linux-x86_64/wheel/proj-0.1.egg-info running install_scripts creating build/bdist.linux-x86_64/wheel/proj-0.1.dist-info/WHEEL 

Затем я установил его в виртуальной среде Python 3:

 $ source ~/.virtualenvs/test_p3/bin/activate (test_p3)$ python setup.py install running install running bdist_egg running egg_info writing dependency_links to proj.egg-info/dependency_links.txt writing proj.egg-info/PKG-INFO writing entry points to proj.egg-info/entry_points.txt writing top-level names to proj.egg-info/top_level.txt reading manifest file 'proj.egg-info/SOURCES.txt' writing manifest file 'proj.egg-info/SOURCES.txt' installing library code to build/bdist.linux-x86_64/egg running install_lib running build_py creating build/bdist.linux-x86_64/egg creating build/bdist.linux-x86_64/egg/src copying build/lib/src/scriptA.py -> build/bdist.linux-x86_64/egg/src copying build/lib/src/__init__.py -> build/bdist.linux-x86_64/egg/src copying build/lib/src/scriptB.py -> build/bdist.linux-x86_64/egg/src byte-compiling build/bdist.linux-x86_64/egg/src/scriptA.py to scriptA.cpython-34.pyc byte-compiling build/bdist.linux-x86_64/egg/src/__init__.py to __init__.cpython-34.pyc byte-compiling build/bdist.linux-x86_64/egg/src/scriptB.py to scriptB.cpython-34.pyc creating build/bdist.linux-x86_64/egg/EGG-INFO copying proj.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO copying proj.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying proj.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying proj.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying proj.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO zip_safe flag not set; analyzing archive contents... creating 'dist/proj-0.1-py3.4.egg' and adding 'build/bdist.linux-x86_64/egg' to it removing 'build/bdist.linux-x86_64/egg' (and everything under it) Processing proj-0.1-py3.4.egg Copying proj-0.1-py3.4.egg to /home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages Adding proj 0.1 to easy-install.pth file Installing scriptA script to /home/desouslu/.virtualenvs/test_p3/bin Installing scriptB script to /home/desouslu/.virtualenvs/test_p3/bin Installed /home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/proj-0.1-py3.4.egg Processing dependencies for proj==0.1 Finished processing dependencies for proj==0.1 

Но когда я запускаю один из скриптов, я получаю следующее:

 (test_p3)$ scriptA Traceback (most recent call last): File "/home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/pkg_resources.py", line 2051, in load entry = getattr(entry,attr) AttributeError: 'module' object has no attribute 'scriptA' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/desouslu/.virtualenvs/test_p3/bin/scriptA", line 9, in <module> load_entry_point('proj==0.1', 'console_scripts', 'scriptA')() File "/home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/pkg_resources.py", line 353, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/pkg_resources.py", line 2321, in load_entry_point return ep.load() File "/home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/pkg_resources.py", line 2053, in load raise ImportError("%r has no %r attribute" % (entry,attr)) ImportError: <module 'src' from '/home/desouslu/.virtualenvs/test_p3/lib/python3.4/site-packages/hex_utils-0.2-py3.4.egg/src/__init__.py'> has no 'scriptA' attribute 

Что именно вызывает эту ошибку?

В вашем случае он ищет scriptA вызываемый в модуле src .

Взглянув на документы , entry_points следует определить следующим образом:

 entry_points={ 'console_scripts': [ 'scriptA=src.scriptA:main_func', 'scriptB=src.scriptB:main_func' ], }, 

Вы должны декларировать свои классы в __init__.py follow link