"AttributeError: 'module' object has no attribute" with installed package

"AttributeError: 'module' object has no attribute" with installed package

我有一个包含这些内容的项目:

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

我正在关注 this guide 为 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 = "luis@mail.com",
    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

究竟是什么导致了这个错误?

您应该在 __init__.py 中声明您的 类 遵循 link

在您的情况下,它会在模块 src.

中查找 scriptA 可调用

查看 docs,entry_points 应该定义为:

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