Can not import .apkg file with Python

Hi, I’m trying to use pylib to manage anki cards/collection/decks/etc.
I use anki==2.1.60

Create .apkg file:

from anki.collection import Collection
collection = Collection('anki.sqlite')
collection.export_anki_package(
    out_path='dump',
    limit=None,
    with_scheduling=True,
    with_media=True,
    legacy_support=False
)

And this code works well.

But when I try to use this dump for import, I get an error: anki.errors.BackendIOError: Failed to create file in '/home/neverdie/projects/test_anki/anki1.sqlite':<br>File exists (os error 17)

from anki.collection import Collection
collection = Collection('anki1.sqlite')
collection.import_anki_package('dump')

I tried to use anki=2.1.56 and it works well, without any errors. Seems like a bug.

Not sure why it would have worked before, but it seems like the collection file extension is hardcoded to be .anki2 and strange things happen if it deviates.

def media_paths_from_col_path(col_path: str) -> tuple[str, str]:
    media_folder = re.sub(r"(?i)\.(anki2)$", ".media", col_path)
    media_db = f"{media_folder}.db2"
    return (media_folder, media_db)
2 Likes

Thanks, it works

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.