gpt4 book ai didi

python - 导入错误 : 'No module named' *does* exist

转载 作者:IT老高 更新时间:2023-10-28 20:29:06 26 4
gpt4 key购买 nike

当我启动金字塔 pserve 时,我得到了这个堆栈跟踪:

% 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

我的 PYTHONPATH 设置为这棵树的根:

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

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

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

(我将最后一行更改为绝对导入。没有运气。)

我尝试过的事情:

  1. 重建虚拟环境

  2. 设置PYTHONPATH

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

  4. Looking for circular imports

我愿意接受有关如何调试此错误的进一步建议。


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

最佳答案

我将 PYTHONPATH 设置为 '.' 并为我解决了这个问题。

export PYTHONPATH='.'

对于单线,您可以轻松做到:

PYTHONPATH='.' your_python_script

这些命令应该在终端中运行

关于python - 导入错误 : 'No module named' *does* exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23417941/

26 4 0