Anki Forums

How to check anki version inside an addon?

Recently Anki is filled with deprecation warnings as:

1788670778\representation\deck.py:97:'joinedFields' is deprecated: please use 'joined_fields'
1788670778\representation\deck_initializer.py:10:'byName' is deprecated: please use 'by_name'
1788670778\representation\deck_config.py:12:'getConf' is deprecated: please use 'get_config'

One way I have been fixing with was with:

join_fields = anki_object.joined_fields if hasattr(anki_object, 'joined_fields') else anki_object.joinedFields

However, I cannot use this for this other warning because there is nothing to check with hasattr:

CrowdAnki\representation\note_model.py:51:flush() is deprecated: no longer required

Is there an Anki version I can check as in this example:

if anki_major > 3 and anki_minor < 40:
    collection.models.flush()

You can use the version variable exported from anki.

from anki import version as anki_version

parts = anki_version.split(".")
major = int(parts[0])
minor = int(parts[1])
point_release = int(parts[2])

if point_release < 40:
    collection.models.flush()
2 Likes

There’s also a anki.utils.point_version() which does similar. If you can convince the add-on author to target only the newer version going forward, that makes things much easier - a new branch can be created that uses the PEP8 syntax, and users who have not upgraded yet can continue to use the previous release of the add-on.