回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试编写一个python脚本,它被分成多个文件,因此我可以更容易地维护它,而不是编写一个很长的单个文件脚本。</p>
<p>以下是目录结构:</p>
<pre><code>wmlxgettext.py
<pywmlx>
|- __init__.py
|- (some other .py files)
|- <state>
|- __init__.py
|- state.py
|- machine.py
|- lua_idle.py
</code></pre>
<p>如果我到达项目的主目录(wmlxgettext.py脚本存储在其中),并且如果我尝试“导入pywmlx”,则会出现导入错误(属性错误:“module”对象没有属性“state”)</p>
<p>以下是完整的错误消息:</p>
<pre><code>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'
</code></pre>
<p>因为我在“项目主目录”中,pywmlx应该在PYTHONPATH上(事实上,当我尝试导入pywmlx/something.py时没有问题)</p>
<p>我不知道我的错误在哪里以及如何解决这个问题。</p>
<p>这里是<strong>pywmlx/\u init.py</strong>源:</p>
<pre><code># 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
</code></pre>
<p>这里是<strong>pywmlx/state/\u init.py</strong>源:</p>
<pre><code>from pywmlx.state.machine import setup
from pywmlx.state.machine import run
</code></pre>
<p>但我认为真正的问题在pywmlx/state目录中存储的一个(或所有)python模块使用的“导入”中隐藏了一些。</p>
<p>这里是<strong>pywmlx/state/machine.py</strong>源:</p>
<pre><code># 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
</code></pre>
<p>最后是<strong>pywmlx/state/lua_idle.py</strong>源代码:</p>
<pre><code>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)
</code></pre>
<p>抱歉,如果我发布了这么多代码和这么多文件。。。但我担心目录中的文件隐藏了不止一个导入问题,所以我将它们全部发布,希望我能解释这个问题,避免混淆。</p>
<p>我想我漏掉了一些关于导入在python中是如何工作的东西,所以我希望这个问题对其他程序员也有用,因为我认为我不是唯一一个在解释导入时发现官方文档很难理解的人。</p>
<hr/>
<p>搜索完成:</p>
<p><a href="https://stackoverflow.com/questions/1917958/python-import-mechanics">Not Useful</a>:我已经在显式地使用import x.y.z了,我需要导入一些东西</p>
<p><a href="https://stackoverflow.com/questions/3201898/python-import-error-no-module-named">Not Useful</a>:即使问题是关于导入错误的,它似乎也没有用,原因与(1)相同</p>
<p><a href="https://stackoverflow.com/questions/3721415/python-import-problem">Not Useful</a>:据我所知,<strong>pywmlx</strong>应该位于PYTHONPATH中,因为我的测试中的“当前工作目录”是包含主python脚本和<strong>pywmlx</strong>目录的目录。如果我错了就纠正我</p>