创建python模块
我创建了两个文件:test.py和test1234.py test.py包含:创建python模块,python,module,Python,Module,我创建了两个文件:test.py和test1234.py test.py包含: import test1234 t = test1234.test() class test(): def __init__(self): test1234.py包含: import test1234 t = test1234.test() class test(): def __init__(self): 当放在同一目录中时,python test.py运行时不会出错 但是,如果我创建一个目
import test1234
t = test1234.test()
class test():
def __init__(self):
test1234.py包含:
import test1234
t = test1234.test()
class test():
def __init__(self):
当放在同一目录中时,python test.py
运行时不会出错
但是,如果我创建一个目录test1234,并将test1234.py和一个空白的init.py放在这个目录中,python test.py
会给出错误:
AttributeError:“模块”对象没有属性“测试”
要使test.py能够在test1234.py中看到
test
类,我需要做什么?您必须通过包导入它,或者将它放入\uu init\uuuuuuuuuu.py
中
import test1234.test1234
t = test1234.test1234.test()
具体来说,在
\uuu init\uuuu.py
中放什么?我只希望在test.py
中有import test1234
。不过,我知道你可以在\uuuu init\uuuuuuuuuuuuuuuuuy
中从test1234导入测试;考虑重命名包还是子模块。我的问题还不够清楚吗?我不理解python导入是如何工作的,这是不能接受的吗?:)