【问题标题】:error while accessing method of class - python package访问类的方法时出错-python包
【发布时间】:2015-04-27 12:29:29
【问题描述】:

我已经上传了一个简单的包到https://testpypi.python.org/pypi。 我已按照以下步骤操作,

python setup.py register -r https://testpypi.python.org/pypi

python setup.py sdist upload -r https://testpypi.python.org/pypi

然后将此包下载到本地并使用 install 命令执行 setup.py。 (包地址https://testpypi.python.org/pypi/printlistonlynew/0.1) 然后包就安装成功了。

这是包的结构和代码,

│   setup.py
│
└───listprint
        stack.py
        __init__.py

stack.py 的内容是,

class Xyz:

    def __init__(self,):
        pass

    def printList(self):
        print [i for i in range(0,10)]

当我尝试从已安装的包中导入方法 printList 时,它给了我错误,

AttributeError: Xyz instance has no attribute 'printList'

这是我要执行的代码,

from listprint import stack
x=stack.Xyz()
print x
print x.printList()

我在这里做错了什么?任何命名空间问题?

【问题讨论】:

  • 请显示尝试执行该导入的代码。
  • @DanielRoseman 请查看更新。

标签: python python-2.7 setuptools


【解决方案1】:

我已经下载了您的文件,但您遇到了缩进问题。因为您混合了制表符和空格,Python 将 printList 视为嵌套在 inside __init__

只保留空格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 2020-09-27
    • 2023-02-18
    相关资源
    最近更新 更多