Importing multiple decks

System info

  • OS: Debian 12
  • Python version: 3.11.2 (using a venv)

Hello everyone,

when I first started using Anki I needed a way to import multiple decks (or even a whole folder) at once. An addon called MultiDeckImporter solved my problem at the time but it seems that now the addon doesn’t work anymore.

So because of that I need to figure out a new solution so I decided to use AnkiConnect and write a Python script which will loop through some folder and import all the .apkg files.

import requests
import sys

def import_deck() -> None:
    anki_url = "http://localhost:8765"
    deck_path = "test.apkg"
    payload = {
        "action": "importPackage",
        "version": 6,
        "params": {
            "path": deck_path
        }
    }

    try:
        response = requests.post(anki_url, json=payload)
        response.raise_for_status()
        print("Deck imported successfully!")
        print(response.json())
    except requests.exceptions.RequestException as e:
        print("An error occurred:", e)
        sys.exit(1)

if __name__ == "__main__":
    import_deck()

Everytime I run my code I get this response:
{'result': None, 'error': "[Errno 2] No such file or directory: 'test.apkg'"}

From the official AnkiConnect documentation I understand that my .apkg files which I want to import have to be inside the collection.media folder.
I have installed Anki through flatpak so the path to my collection.media folder is /home/*username*/.var/app/net.ankiweb.Anki/data/Anki2/User 1/collection.media . However, when I put my test.apkg there the same error occurs…
What am I doing wrong here?

My second idea was to use the anki python module and import decks with this code:

from anki.importing.apkg import AnkiPackageImporter

apkg_path = "test.apkg"
importer = AnkiPackageImporter(apkg_path)
importer.run()

But when I ran the code this gets printed out:
cannot import name 'NotetypeDict' from partially initialized module 'anki.models' (most likely due to a circular import)

1 Like

My problem was solved on another post titled “Bug report - python anki module circular import” under the category Add-ons.

I am writing it like that because I cannot add links…

1 Like

OP’s problem seems to have already been resolved, but I fixed that add-on.

1 Like