【发布时间】:2019-05-17 16:08:10
【问题描述】:
试图编写一个 python 包,但我无法在我的一个源文件中创建一个类的实例。
包装布局是:
-packagedir
----README.md
----setup.py
----packagename
--------__init__.py
--------package.py
--------modules
------------file1.py
------------file2.py
在 init.py 中的包名中我有:
from . modules import file1
from . modules import file2
文件file1.py包含一个类:
class File1():
def __init__(self):
self.val = 0
# Other methods and such
文件file2.py包含一个类:
class File2():
def __init__(self):
self.type = 0
# Other methods and such
在 package.py 我有一个这样的类:
class Aclass(file1.File1, file2.File2):
def __init__(self):
# nothing important in here yet
我已经像这样构建并安装了我的包:
python3 setup.py sdist
sudo pip3 install dist/package-0.1.tar.gz
现在我创建一个名为 test.py 的文件并在其中放入以下内容:
import package
iss = package.Aclass()
当我运行测试文件时,我收到以下错误:
AttributeError: module 'usbiss' has no attribute 'Aclass'
我不明白为什么 python 不允许我创建类 Aclass 的实例并认为我正在访问一个属性。我确信我的导入语句或某些东西存在根本性错误,但我不知道它是什么。如何更正此问题,以便创建 Aclass 的实例并使用其方法?
谢谢。
【问题讨论】:
标签: python python-3.x python-import python-module python-packaging