导入错误:'没有名为'的模块*确实*存在
我在启动 pyramid 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 的交互式环境中,这个是可以正常工作的:
% 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 的虚拟环境中运行这个程序。今天我一直在尝试让它工作,但总是时好时坏,我搞不清楚问题出在哪里。比如说,__init__.py
文件似乎对一些在它之前的导入是没问题的:
from .database import get_db
from .config import parser
from .views import Endpoints, route
(我把最后一行改成了绝对导入,但还是不行。)
我尝试过的解决方法有:
重建虚拟环境
设置
PYTHONPATH
在代码中使用绝对路径
我欢迎更多的建议来调试这个错误。
我犯的错误是只关注了源代码树。其实问题出在运行环境,也就是我的虚拟环境中。当我查看那里时,发现所需的文件并没有被安装。根本问题在于 setup.py
文件。
13 个回答
你的 PYTHONPATH
没有设置好。你可以通过运行 export PYTHONPATH=$PYTHONPATH:/path/to/your/modules
来设置它。
我遇到了同样的问题,我在出错的那一行之前用了
pdb.set_trace()
来调试。我的问题是包的名字和模块的名字重复了,比如:
test
├── __init__.py
├── a
│ ├── __init__.py
│ └── test.py
└── b
└── __init__.py
在文件 a/__init__.py
中,使用 from test.b import xxx
会导致 ImportError: No module named b
的错误。
我也遇到过同样的问题,我是通过在Python文件的顶部添加以下代码来解决的:
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
使用os.path.dirname
的次数取决于你的文件在项目结构中的位置。例如,在我的情况下,项目的根目录在上面三层。
我把 PYTHONPATH
设置成了 '.'
,这样就解决了我的问题。
export PYTHONPATH='.'
如果你想用一句话来解决,也可以这样做:
PYTHONPATH='.' your_python_script
这些命令需要在终端中运行
我通常的做法是在出现导入问题的地方,直接打印出 sys.path
的内容。就你这个情况来说,打印的地方应该是在 /home/hughdbrown/.local/bin/pserve
这个文件里。然后你可以查看这个路径中显示的目录和文件。
你可以先这样做:
import sys
在 Python 3 中使用打印函数:
print(sys.path)
或者在 Python 2 中使用打印表达式:
print sys.path