Python 如何将子模块中的类/函数相互导入?
假设我有一个python模块,如下所示:Python 如何将子模块中的类/函数相互导入?,python,python-module,Python,Python Module,假设我有一个python模块,如下所示: setup.py tpkg/ __init__.py module1.py module2.py 其中\uuuu init\uuuu.py为: from .module1 import class1 from .module2 import class2 from .module2 import class2 class class1(object): def __init__(self): self
setup.py
tpkg/
__init__.py
module1.py
module2.py
其中\uuuu init\uuuu.py
为:
from .module1 import class1
from .module2 import class2
from .module2 import class2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(class2()))
from .module1 import class1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(class1()))
from setuptools import setup
setup(name="tpkg",
version="0.0.1",
packages=["tpkg"])
module1.py
是:
from .module1 import class1
from .module2 import class2
from .module2 import class2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(class2()))
from .module1 import class1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(class1()))
from setuptools import setup
setup(name="tpkg",
version="0.0.1",
packages=["tpkg"])
module2.py
是:
from .module1 import class1
from .module2 import class2
from .module2 import class2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(class2()))
from .module1 import class1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(class1()))
from setuptools import setup
setup(name="tpkg",
version="0.0.1",
packages=["tpkg"])
而setup.py
是:
from .module1 import class1
from .module2 import class2
from .module2 import class2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(class2()))
from .module1 import class1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(class1()))
from setuptools import setup
setup(name="tpkg",
version="0.0.1",
packages=["tpkg"])
因此,这里两个模块都需要导入包含在另一个模块中的类
如果我安装此模块并尝试导入tpkg,则会出现错误
In [1]: import tpkg
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-834a70240c6c> in <module>()
----> 1 import tpkg
build/bdist.linux-x86_64/egg/tpkg/__init__.py in <module>()
build/bdist.linux-x86_64/egg/tpkg/module1.py in <module>()
build/bdist.linux-x86_64/egg/tpkg/module2.py in <module>()
ImportError: cannot import name class1
[1]中的:导入tpkg
---------------------------------------------------------------------------
ImportError回溯(最近一次呼叫最后一次)
在()
---->1进口tpkg
在()
build/bdist.linux-x86_64/egg/tpkg/module1.py in()
build/bdist.linux-x86_64/egg/tpkg/module2.py in()
ImportError:无法导入名称class1
所以,我的问题是,我应该如何将两个模块中的类相互导入
更新这个问题与给出的问题略有不同,它询问循环导入为什么会有问题,而不是如何解决循环导入的问题,这正是我所需要的。从相对路径导入到绝对路径导入,从
到。。。导入…
syntax到import…
(在模块文件中)应该可以解决您的问题
\uuuu init\uuuuu.py
from tpkg.module2 import class2
from tpkg.module1 import class1
import tpkg.module2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module2.class2))
import tpkg.module1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module1.class1))
module1.py
from tpkg.module2 import class2
from tpkg.module1 import class1
import tpkg.module2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module2.class2))
import tpkg.module1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module1.class1))
module2.py
from tpkg.module2 import class2
from tpkg.module1 import class1
import tpkg.module2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module2.class2))
import tpkg.module1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module1.class1))
测试:
>>导入tpkg
>>>c=tpkg.module1.Class1()
>>>c
0x10535ffd0处的
>>>c.其他类别()
>>>
从相对路径导入移动到绝对路径,从移动到。。。导入…
syntax到import…
(在模块文件中)应该可以解决您的问题
\uuuu init\uuuuu.py
from tpkg.module2 import class2
from tpkg.module1 import class1
import tpkg.module2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module2.class2))
import tpkg.module1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module1.class1))
module1.py
from tpkg.module2 import class2
from tpkg.module1 import class1
import tpkg.module2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module2.class2))
import tpkg.module1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module1.class1))
module2.py
from tpkg.module2 import class2
from tpkg.module1 import class1
import tpkg.module2
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module2.class2))
import tpkg.module1
class class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
print(str(tpkg.module1.class1))
测试:
>>导入tpkg
>>>c=tpkg.module1.Class1()
>>>c
0x10535ffd0处的
>>>c.其他类别()
>>>
您通常应该避免循环依赖:
否则,您可以尝试本地导入
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
from test2 import class2
print(str(class2()))
test = class1()
test.otherclass()
及
通常应尽量避免循环依赖关系: 否则,您可以尝试本地导入
class class1(object):
def __init__(self):
self._who = "Class 1"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
from test2 import class2
print(str(class2()))
test = class1()
test.otherclass()
及
可能是@Ivan的副本。这个问题只问为什么会发生导入错误。这个问题是问如何避免这个错误。可能是@Ivan的重复。这个问题只问为什么会发生导入错误。这个问题是问如何避免这个错误。事实上,我以前的回答很愚蠢,什么也没修正。但我认为我的新答案可以解决您的问题,而不必使用本地导入。事实上,我以前的答案很愚蠢,没有解决任何问题。但我认为我的新答案可以解决您的问题,而不必使用本地导入。