我有这些模块和包:
main.py/
pkg1/
__init__.py
maclasse1.py
pkg2/
__init__.py
maclasse2.py
main.py:
from pkg1 import maclasse1
from pkg2 import maclasse2
if __name__=="__main__":
MC1=maclasse1.MaClasse1()
MC1.ma_classe1()
MC2=maclasse2.MaClasse2()
MC2.ma_classe2()
maclasse1.py
from pkg2 import maclasse2
class MaClasse1:
def ma_classe1(self):
print "Ma Classe 1"
c2=maclasse2.MaClasse2()
c2.ma_classe2()
maclasse2.py
from pkg1 import maclasse1
class MaClasse2:
def ma_classe2(self):
print"Ma Classe 2"
mc1=maclasse1.MaClasse1()
mc1.ma_classe1()
当我运行 main.py 文件时,出现此错误:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from pkg1 import maclasse1
File "/home/nakkini/Desktop/tester/pkg1/maclasse1.py", line 1, in <module>
from pkg2 import maclasse2
File "/home/nakkini/Desktop/tester/pkg2/maclasse2.py", line 1, in <module>
from pkg1 import maclasse1
ImportError: cannot import name maclasse1
如何解决此问题?
修改
在Tim的评论之后,我在tohelp.py
中创建pkg3
,导入maclasse1.py
而maclass2.py
导入tohelp.py
,但我仍然遇到同样的问题。
答案 0 :(得分:0)
如果您不打算更改架构,请尝试以下操作:
<强> maclasse1.py 强>
class MaClasse1:
def ma_classe1(self):
from pkg2 import maclasse2
print "Ma Classe 1"
c2=maclasse2.MaClasse2()
c2.ma_classe2()
<强> maclasse2.py 强>
class MaClasse2:
def ma_classe2(self):
from pkg1 import maclasse1
print"Ma Classe 2"
mc1=maclasse1.MaClasse1()
mc1.ma_classe1()