一尘不染

导入错误:“没有命名模块” *存在*

python

我开始金字塔式保护区时会得到以下堆栈跟踪:

% 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

从python REPL可以正常工作:

% 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
>>>

并从命令行导入:

% python -c "from ponder.server.views import Endpoints, route"

删节的tree输出显示了我正在使用的内容:

% tree
├── __init__.py
├── ponder
│   ├── __init__.py
│   ├── server
│   │   ├── __init__.py
│   │   └── views
│   │       ├── environment_templates.py
│   │       ├── groups.py
│   │       ├── __init__.py
│   │       ├── instances.py
│   │       ├── tasks.py
│   │       └── users.py

MyPYTHONPATH设置为该树的根:

% echo $PYTHONPATH
/home/hughdbrown/workspace/ept/ponder/lib

我在使用python 2.7的virtualenv中运行它。今天我一直在进行此工作,但我不知道问题出在哪里。一方面,对于之前出现的 一些
导入__init__.py似乎还可以: __

from .database import get_db
from .config import parser
from .views import Endpoints, route

(我将最后一行更改为绝对导入。不走运。)

我尝试过的事情:

  1. 重建virtualenv

  2. 设置 PYTHONPATH

  3. 在代码中使用绝对路径

我愿意就如何调试此错误提供进一步的建议。


所以我犯的错误是只看源代码树。问题确实出在我的virtualenv中的运行时环境中。当我看那里时,发现没有安装所需的文件。问题的根源在于setup.py


阅读 120

收藏
2020-12-20

共1个答案

一尘不染

我通常的技巧是仅sys.path在发生导入问题的实际环境中进行打印。在您的情况下,似乎打印的位置在/home/hughdbrown/.local/bin/pserve。然后在路径显示的位置检查目录和文件。

为此,您首先需要:

import sys

并在带有打印表达式的python 2中:

print sys.path

或在带有打印功能的python 3中:

print(sys.path)
2020-12-20