Python导入错误:“module”对象没有属性“x”

2024-05-26 07:46:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个python脚本,它被分成多个文件,因此我可以更容易地维护它,而不是编写一个很长的单个文件脚本。

以下是目录结构:

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

如果我到达项目的主目录(wmlxgettext.py脚本存储在其中),并且如果我尝试“导入pywmlx”,则会出现导入错误(属性错误:“module”对象没有属性“state”)

以下是完整的错误消息:

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/\u 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/\u 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)

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

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


搜索完成:

Not Useful:我已经在显式地使用import x.y.z了,我需要导入一些东西

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

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


Tags: frompyimportself目录initsetupmachine
2条回答

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']模块对象的引用,在函数和方法中使用该引用之前,不需要解析属性引用。

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

相关问题 更多 >