【发布时间】:2016-06-27 18:17:58
【问题描述】:
在 Python 3.4(我的操作系统是 Ubuntu/Linux 14.04)中,我有一个包含许多模块的包。其中一些模块相互依赖(模块 A 使用模块 B 中的对象,模块 B 使用模块 A 中的对象)。我可以通过将import Package.A 放在模块B 的顶部和import Package.B 放在模块A 的顶部来管理循环导入。通过使用类的完整包路径执行对来自另一个模块的类的模块内调用,如x = Package.A.SomeClass()。
我在以下两种情况下遇到问题:
案例 1:当模块 B 调用模块 A 中的类以在模块 B 中定义模块级变量时。
案例 2:当模块 B 调用模块 A 中的类来定义模块 B 中的类级变量时。
我想了解为什么 Package.A.SomeClass() 在某些情况下有效而在其他情况下无效。
我在下面复制了一个非常简化的代码版本(我的真实代码可以包含来自 3 或 4 个模块的依赖项)。
我的包裹结构:
/home/phodor/test.py
/home/phodor/MInventory
/home/phodor/MInventory/__init__.py (empty file)
/home/phodor/MInventory/MProducts.py
/home/phodor/MInventory/MStores.py
一般情况:以下效果很好
/home/phodor/MInventory/MProducts.py
import MInventory.MStores
class MRobot():
def __init__(self):
self.Price = 0
self.Weight = 0
/home/phodor/MInventory/MStores.py
import MInventory.MProducts
class MCommercialCenterStore():
def __init__(self):
self.Name = ""
self.CommercialCenter = ""
self.Products = []
self.MostSoldProduct = MInventory.MProducts.MRobot()
/home/phodor/test.py
import sys
sys.path.append("/home/phodor/")
from MInventory.MProducts import *
from MInventory.MStores import *
oStore = MCommercialCenterStore()
oStore.Name = "Main Street SuperCenter"
print(oStore.Name)
print(oStore.MostSoldProduct)
案例 1:以下产生错误。
/home/phodor/MInventory/MStores.py
import MInventory.MProducts
CustomersChoice = MInventory.MProducts.MRobot
class MCommercialCenterStore():
def __init__(self):
self.Name = ""
self.CommercialCenter = ""
self.Products = []
self.MostSoldProduct = CustomersChoice
错误:
CustomersChoice = MInventory.MProducts.MRobot
AttributeError: 'module' object has no attribute 'MProducts'
案例 2:以下也会产生错误。
/home/phodor/MInventory/MStores.py
class MCommercialCenterStore():
MostSoldProduct = MInventory.MProducts.MRobot
def __init__(self):
self.Name = ""
self.CommercialCenter = ""
self.Products = []
错误:
MostSoldProduct = MInventory.MProducts.MRobot
AttributeError: 'module' object has no attribute 'MProducts'
循环依赖信息
如前所述,我的包包含循环依赖项。到目前为止,解决这个问题的唯一方法是在我的包模块的开头使用以下语句:
import MInventory.Mproducts ----> in the MStores module
import MInventory.MStores ----> in the MProducts module
由于循环依赖,使用以下语句会导致错误:
from MInventory import MProducts
from MInventory import MStores
ImportError: cannot import name 'MProducts'
知道为什么MInventory.MProducts.MRobot 可以在一般情况下正确调用,为什么它在作为模块级变量或类级变量调用时不起作用?
【问题讨论】:
标签: python-3.x import package global-variables class-variables