Python error: ImportError: cannot import name Akismet

前端 未结 6 1066
时光说笑
时光说笑 2021-02-13 20:40

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

6条回答
  •  感动是毒
    2021-02-13 21:21

    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.

提交回复
热议问题