Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python error: ImportError: cannot import name Akismet

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
like image 245
Doppelganger Avatar asked Feb 28 '10 17:02

Doppelganger


People also ask

How to fix ImportError cannot import name from?

The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.

Can t import name python?

In Python "ImportError: cannot import name" error generally occurs when the imported class is not accessible, or the imported class is in a circular dependency. The following are the major reasons for the occurrence of "ImportError: cannot import name": The imported class is in a circular dependency.

How to fix import module error in python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .


1 Answers

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.

like image 110
david.barkhuizen Avatar answered Sep 22 '22 19:09

david.barkhuizen