【问题标题】:Python import error: 'module' object has no attribute 'x'Python 导入错误:“模块”对象没有属性“x”
【发布时间】:2016-05-01 17:28:25
【问题描述】:

我正在尝试做一个python脚本,它被分成多个文件,这样我可以更容易地维护它,而不是制作一个很长的单个文件脚本。

目录结构如下:

wmlxgettext.py
<pywmlx>
  |- __init__.py
  |- (some other .py files)
  |- <state>
       |- __init__.py
       |- state.py
       |- machine.py
       |- lua_idle.py

如果我到达我的项目的主目录(存储 wmlxgettext.py 脚本的地方)并且如果我尝试“导入 pywmlx”我有一个导入错误(属性错误:“模块”对象没有属性“状态” )

这是完整的错误信息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/programmi/my/python/wmlxgettext/true/pywmlx/__init__.py", line 9, in <module>
    import pywmlx.state as statemachine
  File "/home/user/programmi/my/python/wmlxgettext/true/pywmlx/state/__init__.py", line 1, in <module>
    from pywmlx.state.machine import setup
  File "/home/user/programmi/my/python/wmlxgettext/true/pywmlx/state/machine.py", line 2, in <module>
    from pywmlx.state.lua_idle import setup_luastates
  File "/home/user/programmi/my/python/wmlxgettext/true/pywmlx/state/lua_idle.py", line 3, in <module>
    import pywmlx.state.machine as statemachine
AttributeError: 'module' object has no attribute 'state'

由于我在“项目主目录”中,所以 pywmlx 应该在 PYTHONPATH 上(事实上,当我尝试导入 pywmlx/something.py 时我没有遇到任何问题)

我无法弄清楚我的错误在哪里以及如何解决这个问题。

这里是 pywmlx/__init__.py 来源:

# all following imports works well:
from pywmlx.wmlerr import ansi_setEnabled
from pywmlx.wmlerr import wmlerr
from pywmlx.wmlerr import wmlwarn
from pywmlx.postring import PoCommentedString
from pywmlx.postring import WmlNodeSentence
from pywmlx.postring import WmlNode 

# this is the import that does not work:
import pywmlx.state as statemachine

这里是 pywmlx/state/__init__.py 来源:

from pywmlx.state.machine import setup
from pywmlx.state.machine import run

但我认为真正的问题在某种程度上隐藏在 pywmlx/state 目录中存储的一个(或所有)python 模块使用的“导入”中。

这里是 pywmlx/state/machine.py 来源:

# State is a "virtual" class
from pywmlx.state.state import State
from pywmlx.state.lua_idle import setup_luastates
import pywmlx.nodemanip as nodemanip

def addstate(self, name, value):
    # code is not important for this question
    pass

def setup():
    setup_luastates()

def run(self, *, filebuf, fileref, fileno, startstate, waitwml=True):
    # to do
    pass

最后是 pywmlx/state/lua_idle.py 来源:

import re
import pywmlx.state.machine as statemachine
# State is a "virtual" class
from pywmlx.state.state import State

# every state is a subclass of State
# all proprieties were defined originally on the base State class:
    # self.regex and self.iffail were "None"
    # the body of "run" function was only "pass"
class LuaIdleState (State):
    def __init__(self):
        self.regex = re.compile(r'--.*?\s*#textdomain\s+(\S+)', re.I)
        self.iffail = 'lua_checkpo'

    def run(xline, match):
        statemachine._currentdomain = match.group(1)
        xline = None
        return (xline, 'lua_idle')


def setup_luastates():
    statemachine.addstate('lua_idle', LuaIdleState)

抱歉,如果我发布了这么多代码和这么多文件...但我担心目录中的文件隐藏了多个导入问题,所以我将它们全部发布,希望我能解释问题避免混淆.

我想我错过了一些关于 import 如何在 python 中工作的东西,所以我希望这个问题对其他程序员也有用,因为我认为我不是唯一一个在解释 import 时发现官方文档很难理解的人.


搜索完成:

Not Useful:我已经明确使用 import x.y.z 任何时候我需要导入一些东西

Not Useful:即使问题是关于导入错误,但由于与(1)相同的原因,它似乎没有用

Not Useful: 据我所知,pywmlx 应该位于 PYTHONPATH 中,因为我的测试中的“当前工作目录”是包含主要 python 脚本和 pywmlx 目录。如果我错了,请纠正我

【问题讨论】:

  • 您可以尝试将有问题的导入更改为from . import state as statemachine吗?
  • 您是否尝试过先处理空 init 文件?如果这可行,请尝试不使用别名(import as),因为这会导致命名空间冲突。我认为您的循环导入名称空间混乱。尽量使用全名(只写 import ...,不写 from imports)。
  • @Serbitar:我想为您的评论“投票”,因为您的建议非常有效。用 pywmlx/state/lua_idle.py 上的 import pywmlx.state.machine 替换 import pywmlx.state.machine as statemachine 效果很好(可能,将 pywmlx.state.machine 别名为 statemachine 与 pywmlx/__init__.py 使用的别名产生冲突,其中 pywmlx.state 别名为 statemachine 也是。我不知道如何标记对您的评论的正面投票:/ 我愿意这样做
  • 谢谢,刚刚添加了答案。

标签: python python-3.x import


【解决方案1】:

Python 在导入包时做了几件事:

  • sys.modules中为包创建一个对象,名称为key:'pywmlx''pywmlx.state''pywmlx.state.machine'
  • 运行为该模块加载的字节码;这可能会创建更多模块。
  • 一旦一个模块完全加载并且它位于另一个包中,将该模块设置为父模块对象的一个​​属性。因此,sys.modules['pywmlx.state'] 模块被设置为sys.modules['pywmlx'] 模块对象的state 属性。

您的示例中尚未执行最后一步,但以下行仅在设置后才有效:

import pywmlx.state.machine as statemachine

因为这首先将statemachine 作为属性查找。请改用此语法:

from pywmlx.state import machine as statemachine

或者,只需使用

import pywmlx.state.machine

并将其他任何地方的statemachine. 替换为pywmlx.state.machine.。这是可行的,因为添加到命名空间的所有内容都是对 sys.modules['pywmlx'] 模块对象的引用,并且在您在函数和方法中使用该引用之前,不需要解析属性引用。

【讨论】:

  • 非常感谢您的回答。我不确定我是否理解了您的所有解释,但是当您尝试以简化的方式解释如此复杂的事情(深入了解导入的工作原理)时,我感谢您所做的出色工作。我没有尝试您的解决方案(可能会很好),因为从 lua_idle.py 上使用的 import 中删除别名也可以,而且它是一个更适合的解决方案我试图用 pywmlx 使用的命名空间来管理 :)
  • @Nobun:当然,这也有效;我已经添加了答案的替代方案。
  • That last step hasn't taken place yet in your example 你认为这是为什么?
  • pywmlx.state 的导入尚未完成。在执行模块太级代码期间,会触发更多导入(在回溯中可见),导致导入仅在 pywmlx.state 导入完全完成时才有效。
【解决方案2】:

您的框架中有一个循环导入。循环导入不适用于别名。当导入一个带有别名的模块,然后在循环导入期间再次导入它而不使用别名时,python 会抱怨。解决方案是不使用别名(“import module as”语法),而是始终使用完整的“import module”语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-29
    • 2019-07-09
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多