本文介绍了“属性错误:‘模块’对象没有属性";有安装包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的项目:

I have a project with these contents:

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

我正在按照本指南打包这个项目对于 PiPY.setup.py 文件看起来像:

I am following this guide to package this project for PiPY. The setup.py file looks like:

#!/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:

The scripts just print text messages. This is 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 虚拟环境中:

I then installed it in a Python 3 virtual environment:

$ 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

但是当我运行其中一个脚本时,我得到以下信息:

But when I run one of the scripts I get the following:

(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

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

What is exactly causing this error?

推荐答案

在你的例子中,它寻找 scriptA 在模块 src 中可调用.

In your case it looks for scriptA callable in module src.

查看文档,entry_points 应定义如下:

Looking at the docs, entry_points should be defined like:

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

这篇关于“属性错误:‘模块’对象没有属性";有安装包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:39