Import anki module 2.1.45,46 cause no attribute '_backend' error

@dae excuse me ! Here are example code

import anki._backend
# or import 
import anki.utils

error output as follows

import anki._backend
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\anki\_backend\", line 14, in <module>
    import anki.lang
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\anki\", line 16, in <module>
    TR = anki._backend.LegacyTranslationEnum
AttributeError: module 'anki' has no attribute '_backend'

Is this a bug ? or am I wrong with some code?
Any advice would be appreciated!

Maybe your versions of the aqt and anki modules do not match.

I use command pip install anki==2.1.46;
Is there version incompitabily in it ?
And then,In my Windows cmd shell,run

import anki.utils

in version 2.1.44 and older,there is no problem.
the error appears,I dont know whether it’s my computer problem or not?

I find the reason why the error appears by compare latest version with 2.1.44.
in anki,there is no code,so I copy some code into it,and it works.

from anki.collection import Collection
__all__ = ["Collection"]

Thanks for your reply! will this be added into file next time?

The file can’t be changed back, as it is used as a namespace now, but your file should work correctly if you put from anki.collection import Collection at the top of it.

1 Like

I also encountered this error, and you can fix it by importing anki.lang first.

import anki.lang
import anki._backend

The reason is because anki._backend imports anki.lang, which has the line TR = anki._backend.LegacyTranslationEnum. But because anki._backend module hasn’t finished initializing, anki._backend attribute hasn’t been set yet.

However the other way around works, because anki.lang is not used in anki._backend until anki.lang finishes initializing. (Because it’s used inside functions that don’t get run before anki.lang is initialized)

UPDATE: Also, for other anki.module import failing because of their order, importing anki.collection first like @ankibaby has suggested is quite useful in importing them in the correct order!

1 Like