回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>当我开始金字塔服务时,我得到了这个堆栈跟踪:</p>
<pre class="lang-none prettyprint-override"><code>% python $(which pserve) ../etc/development.ini
Traceback (most recent call last):
File "/home/hughdbrown/.local/bin/pserve", line 9, in <module>
load_entry_point('pyramid==1.5', 'console_scripts', 'pserve')()
File "/home/hughdbrown/.virtualenvs/ponder/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/scripts/pserve.py", line 51, in main
return command.run()
File "/home/hughdbrown/.virtualenvs/ponder/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/scripts/pserve.py", line 316, in run
global_conf=vars)
File "/home/hughdbrown/.virtualenvs/ponder/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/scripts/pserve.py", line 340, in loadapp
return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 247, in loadapp
return loadobj(APP, uri, name=name, **kw)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 271, in loadobj
global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 296, in loadcontext
global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 320, in _loadconfig
return loader.get_context(object_type, name, global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 454, in get_context
section)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 476, in _context_from_use
object_type, name=use, global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 406, in get_context
global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 296, in loadcontext
global_conf=global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 337, in _loadfunc
return loader.get_context(object_type, name, global_conf)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/loadwsgi.py", line 681, in get_context
obj = lookup_object(self.spec)
File "/home/hughdbrown/.virtualenvs/ponder/lib/python2.7/site-packages/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/util.py", line 68, in lookup_object
module = __import__(parts)
File "/home/hughdbrown/.virtualenvs/ponder/local/lib/python2.7/site-packages/ponder-0.0.40-py2.7.egg/ponder/server/__init__.py", line 10, in <module>
from ponder.server.views import Endpoints, route
ImportError: No module named views
</code></pre>
<p>这在python REPL中工作得很好:</p>
<pre class="lang-none prettyprint-override"><code>% python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ponder.server.views import Endpoints, route
>>>
</code></pre>
<p>从命令行导入:</p>
<pre class="lang-none prettyprint-override"><code>% python -c "from ponder.server.views import Endpoints, route"
</code></pre>
<p>简略的<code>tree</code>输出显示了我正在使用的内容:</p>
<pre class="lang-none prettyprint-override"><code>% tree
├── __init__.py
├── ponder
│ ├── __init__.py
│ ├── server
│ │ ├── __init__.py
│ │ └── views
│ │ ├── environment_templates.py
│ │ ├── groups.py
│ │ ├── __init__.py
│ │ ├── instances.py
│ │ ├── tasks.py
│ │ └── users.py
</code></pre>
<p>我的<code>PYTHONPATH</code>设置为此树的根:</p>
<pre class="lang-none prettyprint-override"><code>% echo $PYTHONPATH
/home/hughdbrown/workspace/ept/ponder/lib
</code></pre>
<p>我在一个使用Python2.7的virtualenv中运行这个。我今天断断续续地做这个,但我不知道问题在哪里。首先,<code>__init__.py</code>对于前面的一些</em>导入似乎没问题:</p>
<pre><code>from .database import get_db
from .config import parser
from .views import Endpoints, route
</code></pre>
<p>(我把最后一行改为绝对导入。不走运。)</p>
<p>我尝试过的事情:</p>
<ol>
<li><p>重建virtualenv</p></li>
<li><p>设置<code>PYTHONPATH</code></p></li>
<li><p>在代码中使用绝对路径</p></li>
<li><p><a href="https://stackoverflow.com/questions/2406007/detecting-circular-imports">Looking for circular imports</a></p></li>
</ol>
<p>对于如何调试此错误的进一步建议,我持开放态度。</p>
<hr/>
<p>所以我犯的错误是只看源代码树。问题实际上是在运行时环境中,在我的virtualenv中。当我看那里时,我发现没有安装所需的文件。问题的根源是<code>setup.py</code>。</p>