即使路径显示正确,我也无法导入模块。
>>> import cms.middleware.page
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named middleware.page
Heres显示我的路径没问题
>>> for x in sys.path:
... if x == "/usr/local/lib/python2.7/site-packages":
... print x
...
/usr/local/lib/python2.7/site-packages
继承模块
[root@monty codecrab]# ls -l /usr/local/lib/python2.7/site-packages/cms/middleware/page.py
-rw-r--r-- 1 root root 1304 Apr 17 19:49 /usr/local/lib/python2.7/site-packages/cms/middleware/page.py
以下是文件
[root@monty cms]# pwd
/usr/local/lib/python2.7/site-packages/cms
[root@monty cms]# ls -l __init__.py middleware/__init__.py middleware/page.py
-rw-r--r-- 1 root root 47 Apr 17 19:49 __init__.py
-rw-r--r-- 1 root root 0 Apr 17 19:49 middleware/__init__.py
-rw-r--r-- 1 root root 1304 Apr 17 19:49 middleware/page.py
有什么想法吗?
答案 0 :(得分:1)
这可能是因为您的包裹安装不正确。
为了能够import cms.middleware.page
,cms
目录必须位于Python路径上,cms
和middleware
目录都包含__init__.py
个文件这样它们就被视为Python包。
必须存在以下文件/目录:
/usr/local/lib/python2.7/site-packages/cms
/usr/local/lib/python2.7/site-packages/cms/__init__.py
/usr/local/lib/python2.7/site-packages/cms/middleware
/usr/local/lib/python2.7/site-packages/cms/middleware/__init__.py
/usr/local/lib/python2.7/site-packages/cms/middleware/page.py