如何将 classes/functions 从子模块导入到另一个?
How to import classes/functions from submodules into one another?
假设我有一个python模块设置如下:
setup.py
tpkg/
__init__.py
module1.py
module2.py
其中 __init__.py
是:
from .module1 import class1
from .module2 import class2
module1.py
是:
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()))
module2.py
是:
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()))
和setup.py
是:
from setuptools import setup
setup(name="tpkg",
version="0.0.1",
packages=["tpkg"])
所以,这里两个模块都需要导入另一个模块中包含的class。
如果我安装此模块并尝试 import 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
所以,我的问题是,我实际上应该如何将两个模块中的 classes 相互导入?
Update这个问题和给定的here略有不同,问的是为什么会出现循环导入的问题,而不是怎么解决的问题循环导入的问题,正是我需要的
从相对路径导入移动到绝对路径,从 from ... import...
syntaxe 移动到 import ...
(在模块文件中)应该可以解决你的问题
__init__.py
from tpkg.module2 import class2
from tpkg.module1 import class1
module1.py
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))
module2.py
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))
测试:
>>> import tpkg
>>> c = tpkg.module1.Class1()
>>> c
< tpkg.module1.Class1 object at 0x10535ffd0>
>>> c.otherclass()
<class 'tpkg.module2.Class2'>
>>>
您通常应该尽量避免循环依赖:
http://stackabuse.com/python-circular-imports/
否则你可以试试本地进口
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 class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
from test1 import class1
print(str(class1()))
假设我有一个python模块设置如下:
setup.py
tpkg/
__init__.py
module1.py
module2.py
其中 __init__.py
是:
from .module1 import class1
from .module2 import class2
module1.py
是:
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()))
module2.py
是:
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()))
和setup.py
是:
from setuptools import setup
setup(name="tpkg",
version="0.0.1",
packages=["tpkg"])
所以,这里两个模块都需要导入另一个模块中包含的class。
如果我安装此模块并尝试 import 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
所以,我的问题是,我实际上应该如何将两个模块中的 classes 相互导入?
Update这个问题和给定的here略有不同,问的是为什么会出现循环导入的问题,而不是怎么解决的问题循环导入的问题,正是我需要的
从相对路径导入移动到绝对路径,从 from ... import...
syntaxe 移动到 import ...
(在模块文件中)应该可以解决你的问题
__init__.py
from tpkg.module2 import class2
from tpkg.module1 import class1
module1.py
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))
module2.py
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))
测试:
>>> import tpkg
>>> c = tpkg.module1.Class1()
>>> c
< tpkg.module1.Class1 object at 0x10535ffd0>
>>> c.otherclass()
<class 'tpkg.module2.Class2'>
>>>
您通常应该尽量避免循环依赖:
http://stackabuse.com/python-circular-imports/
否则你可以试试本地进口
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 class2(object):
def __init__(self):
self._who = "Class 2"
def __str__(self):
return "I'm {}".format(self._who)
def otherclass(self):
from test1 import class1
print(str(class1()))