Cannot import Collection

Hi, I’m trying to make a code which automatically adds 2 cards, both using the same inputs for front and back, but one with the answer at front. Given is for trying to learn chinese, I need that one card shows me the pinyin and translation at the front, while the pictogram is in the answer and viceversa. The code should add a tag, “Pinyin” or “Pictograma”, according to which thing is being asked in the front card.
Thing is, I tried to do the coding by using chatGPT and the little bit I know from python (version 3.10.6) and I have this code:

import sys
sys.path.append('/usr/share/anki')

from anki import Collection

# Input the values for Pinyin and Pictograma
pinyin = input("Enter the Pinyin: ")
pictograma = input("Enter the Pictograma: ")

col = Collection(r"collection.anki2directory")

# Create Pinyin card
model = col.models.byName("Pinyin")
note = col.newNote(model)
note["Pinyin"] = pinyin
note["Pictograma"] = pictograma
note.addTag("Pinyin")
col.addNote(note)

# Create Pictograma card
model = col.models.byName("Pictograma")
note = col.newNote(model)
note["Pinyin"] = pinyin
note["Pictograma"] = pictograma
note.addTag("Pictograma")
col.addNote(note)

col.save()
col.close()

But when I run it, the following error appears:

ImportError: cannot import name 'Collection' from 'anki' (unknown location)

By changing Collection to collection in the import command, this error dissapears, but another one pops up:

col = collection(r"collection.anki2directory")
TypeError: 'module' object is not callable

The directory of the collection.anki2 file is fine, I just modified for obvious reasons.

Any help on fixing the code or getting an alternative solutions is really aprecciated.

Nvm, It can be done by making a new note type by cloning the inverse card option and using the deck override along with subdecks.

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