0

Im unable to import a module even though my path appears correct.

>>> import cms.middleware.page
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named middleware.page

Heres shows my path is ok

>>> for x in sys.path:
...     if x == "/usr/local/lib/python2.7/site-packages":
...         print x
...
/usr/local/lib/python2.7/site-packages

Heres the module

[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

Here are the files

[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

Any ideas ?

2
  • what is the output of echo $PATH Commented Apr 18, 2013 at 7:46
  • What does 'import cms; print cms.__path__' output? Commented Apr 18, 2013 at 13:23

1 Answer 1

1

This is likely due to an incorrect installation of your package.

To be able to do import cms.middleware.page, the cms directory must be on your Python path, with both cms and middleware directories containing an __init__.py file so that they are treated as a Python package.

The following files/directories must exist:

/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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, but ive just checked and all the files are there. question updated
@felix001 For reference, if all of the files were in place, what was causing the problem with the failed import?
I had created a app called cms as well by confusion, which I think may of been polluting the namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.