Home » Blog » Python Module On Import Path Won't Import

Python Module on Import Path Won't Import

I just ran into an interesting issue whilst implementing Akismet spam protection on my contact form. Although the akismet.py module was on my import path, the module wouldn’t import in a python shell.

What I’d done is downloaded the module from Void Space to my local machine and then done an SCP to my home folder on my server. From there I’d copied it onto the Python import path at /usr/lib/python2.5/site-packages. If I started a shell from my home folder, the module would import fine but if I started one from /usr/lib/python2.5/site-packages or anywhere else, it wouldn’t.

Turns out that the permissions on the file were set incorrectly. Running an ls -l returned that only the owner had read and write permissions.

-rw———- 1 root root 13457 Dec 13 23:45 akismet.py

I’m no expert but I’m assuming that if you want to import the module it needs to have ‘other’ permissions. I just set them to read-write-execute for everyone and everything worked fine.

sudo chmod 777 /usr/lib/python2.5/site-packages/akismet.py

3 Comments

same problem : module exists, directory entered into PYTHONPATH, refuses to import. permissions are correct and your fix did not help.
For reasons I do not understand, it only accepts imports from modules I place in my home directory. If I try to keep my modules in a subdirectory that is not my home directory they can not be imported.
When you launch a Python shell, the first import path is the current directory. Are you launching the python shell from your home directory, i.e. the default path when you launch terminal?

Add Your Opinions

Latest Tweet

Recent Posts

Elsewhere