0

即使我的路径看起来正确,我也无法导入模块。

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

有任何想法吗 ?

4

1 回答 1

1

这可能是由于您的软件包安装不正确。

为了能够做到这一点import cms.middleware.page,该cms目录必须在您的 Python 路径上,并且cmsmiddleware目录都包含一个__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
于 2013-04-18T08:17:26.283 回答