污妖王 发表于 2018-9-27 11:15:48

ImportError: No module named views

当我开始使用pyramid pserve时,我得到了错误
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

I_Like_AI 发表于 2018-9-27 11:19:32

本帖最后由 I_Like_AI 于 2018-9-27 11:30 编辑

我通常的诀窍就是打印sys.path。导入问题发生的实际上下文中的路径。在你的情况下,似乎打印的地方在/home/hughdbrown/.local/bin/pserve . T
import sys
python2版本
print sys.path
python3版本
print(sys.path)

黑崎一护 发表于 2018-9-27 11:21:29

本帖最后由 黑崎一护 于 2018-9-27 11:22 编辑

我将PYTHONPATH设置为 '.'通过下面命令
export PYTHONPATH='.'





马猴烧酒 发表于 2018-9-27 11:31:55

我遇到了同样的问题,我通过在python文件的顶部添加以下代码来解决这个问题:import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))


强人锁男 发表于 2018-9-27 11:38:10

我遇到了同样的问题,pdb.set_trace()。我的问题是包名与模块名重复,比如:test
├── __init__.py
├── a
│   ├── __init__.py
│   └── test.py
└── b
    └── __init__.py

在文件a/__init__.py,使用using from test.b import xxx将导致ImportError:没有命名为b的模块。

德国骨科 发表于 2018-9-27 11:43:47

如果在另一个目录中有一个与模块同名的脚本,它将使用该脚本。例如:
module.py

module
|
|--module
||
||--__init__.py
||--module.py

第一个module.py将被使用,第二个module.py不会被使用

3529956381 发表于 2018-10-9 08:10:30

谢谢受教了

lovelylucky 发表于 2018-10-9 08:24:10

嗯嗯谢谢楼主,以防以后遇到

菜鸟闯世界 发表于 2018-10-10 08:09:41

谢谢!大爱啊

爱心觉罗 发表于 2018-10-15 08:50:03

谢谢楼主受教了
页: [1] 2
查看完整版本: ImportError: No module named views