【发布时间】: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