Question

I've seen many similar errors, but I can't see a solution that applies to my particular problem.

I'm trying to use the Akismet module which is on my PYTHONPATH, then if I start up the interactive interpreter, when I run from akismet import Akismet (as the docstring says), I get the following error:

from akismet import Akismet
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name Akismet
Was it helpful?

Solution

It will work perfectly if your PYTHONPATH is set correctly and globally (just tested it myself).

  • Must be set to the directory containing "akismet.py", not the file path! Make sure you don't use relative paths.
  • Note that you might need to reboot/logoff in order to apply environment variable changes to all programs.

OTHER TIPS

I just want to draw more attention to Doppelganger's own answer to his question. I had this error, and the situation is this:

You're trying to import function/class X from a module called say 'strategy.py'.

Unfortunately you've also created a python package directory called strategy, in other words you've got a directory called 'strategy', with at least a single file in directory 'strategy' called '____init___.py'.

root folder\
    strategy.py (contains function/class called X)
    strategy\
        __init__.py

You then forget about the fact that you've created the python package directory, and try to import some class or function defined in file strategy.py in the 'root' directory, like so

from strategy import X

What you then get is the Python error: ImportError: cannot import name X error.

The actual problem, as Doppelganger notes, is that the python interpretor gives precedence to the package directory that you've created, and searches for a FILE/MODULE named X in the package directory, and ignores the actual module strategy.py, and function/class X therein that you're actually looking for.

This is exactly what you'd expect and want if you read the documentation on python packages, but if you change your mind halfway along like I did, you may end up scratching your head.

Check if your PYTHONPATH is really what you expect it to be, e.g. by doing this in an interactive console:

In [1]: import sys

In [2]: print sys.path

is akismet.py really in one of those folders?

You should have the directory containing the 'akismet' directory in your path. I guess, you have added the 'akismet' directory itself to $PYTHONPATH.

When you write:

from akismet import Akismet

Python tries to open file akismet/Akismet.py somewhere in its search path.

All this assuming Akismet is a file and akismet is a directory. If there is an akismet.py file, then the directory containing this file should be listed in $PYTHONPATH.

Simple:

  1. Make sure you have installed akismet (system-wide or virtualenv)
  2. Verify by pasting (import akismet) into a python shell
  3. CD into the root directory of your project and run (find . -name akismet.py)
  4. If you find a file with that name, see if you can rename it
  5. Then run (find . -name akismet.pyc -print0 | xargs -0 rm -rf)

Now run your application and you should be good to go.

I always forget that ipython imports the modules when you use run command inside the ipython interpreter. It won't re-import any modules that you change, so any new variables or functions won't be found. This is a known issue with ipython.

Conclusion: Avoid using run as it won't reload your modules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top