Python 3.x Python 3.4-当用作模块级变量或类级变量时,来自另一个模块的类会导致错误

Python 3.x Python 3.4-当用作模块级变量或类级变量时,来自另一个模块的类会导致错误,python-3.x,import,package,global-variables,class-variables,Python 3.x,Import,Package,Global Variables,Class Variables,在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中的模

在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()
import MInventory.MProducts

CustomersChoice = MInventory.MProducts.MRobot

class MCommercialCenterStore():    
    def __init__(self):       
        self.Name = ""
        self.CommercialCenter = ""
        self.Products = []
        self.MostSoldProduct = CustomersChoice
class MCommercialCenterStore():
   MostSoldProduct = MInventory.MProducts.MRobot
   def __init__(self):   
      self.Name = ""
      self.CommercialCenter = ""
      self.Products = []
/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

class MCommercialCenterStore():    
   def __init__(self):       
      self.Name = ""
      self.CommercialCenter = ""
      self.Products = []
      self.MostSoldProduct = MInventory.MProducts.MRobot()
import MInventory.MProducts

CustomersChoice = MInventory.MProducts.MRobot

class MCommercialCenterStore():    
    def __init__(self):       
        self.Name = ""
        self.CommercialCenter = ""
        self.Products = []
        self.MostSoldProduct = CustomersChoice
class MCommercialCenterStore():
   MostSoldProduct = MInventory.MProducts.MRobot
   def __init__(self):   
      self.Name = ""
      self.CommercialCenter = ""
      self.Products = []
错误:

CustomersChoice = MInventory.MProducts.MRobot
AttributeError: 'module' object has no attribute 'MProducts'
MostSoldProduct = MInventory.MProducts.MRobot
AttributeError: 'module' object has no attribute 'MProducts'
情况2:以下情况也会产生错误

/home/phodor/MInventory/MStores.py

import MInventory.MProducts

class MCommercialCenterStore():    
   def __init__(self):       
      self.Name = ""
      self.CommercialCenter = ""
      self.Products = []
      self.MostSoldProduct = MInventory.MProducts.MRobot()
import MInventory.MProducts

CustomersChoice = MInventory.MProducts.MRobot

class MCommercialCenterStore():    
    def __init__(self):       
        self.Name = ""
        self.CommercialCenter = ""
        self.Products = []
        self.MostSoldProduct = CustomersChoice
class MCommercialCenterStore():
   MostSoldProduct = MInventory.MProducts.MRobot
   def __init__(self):   
      self.Name = ""
      self.CommercialCenter = ""
      self.Products = []
错误:

CustomersChoice = MInventory.MProducts.MRobot
AttributeError: 'module' object has no attribute 'MProducts'
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不能在模块名和要使用的函数或类名之间存在差异

这个问题很常见,但它可以以许多不同的形式出现。在这里,您尝试进入一个名为
MInventory
的文件。此文件不存在,因此也没有模块。这就产生了问题

也可能是您必须以另一种方式导入模块。尝试:

      from MInventory import *

我尝试过其他代码示例,效果很好。如果它不起作用,我也不知道它可能是什么


诚恳地说,heureka

我以前尝试过这些说法。但是,通过使用“from MInventory import MStores”返回到循环依赖性问题中,我得到了以下错误:“ImportError:无法导入名称‘MStores’”。解决模块中循环依赖关系的唯一方法是使用import MInventory.MProducts,并在需要从MProducts调用类时使用完整路径表示法,如MInventory.MProducts.MRobot。这很烦人,因为完整路径表示法在类函数中可以正常工作,但在类函数之外的任何地方都会导致“module has no attribute”(模块没有属性)错误。因此,恐怕我知道没有解决方案!:-(