Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Note
✓ from genanki import Note
Deck
✓ from genanki import Deck
Package
✓ from genanki import Package
Model
✓ from genanki import Model
Creates a simple Anki deck with one card and writes it to output.apkg.
import genanki
# Define a model (card template)
my_model = genanki.Model(
1607392319,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
# Create a deck with a unique ID
deck = genanki.Deck(
2059400110,
'My Deck')
# Add notes (cards) to the deck
note = genanki.Note(
model=my_model,
fields=['Capital of France?', 'Paris'])
deck.add_note(note)
# Generate .apkg file
package = genanki.Package(deck)
package.write_to_file('output.apkg')
print('Deck generated successfully!')
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'genanki'
Package not installed; or virtual environment not activated.
fixRun: pip install genanki
ValueError: Model ID 1607392319 is already used by another model in this deck.
Reusing a Model ID that conflicts with an existing one in the same deck (or across decks).
fixUse a unique ID for each model, e.g., import random; model_id = random.randrange(1<<30, 1<<31)
TypeError: write_to_file() missing 1 required positional argument: 'filename'
Forget to pass filename to write_to_file; it's a method, not a property.
fixpackage.write_to_file('deck.apkg') Upgrade
Version history
0.13.1latest on PyPI · released Nov 12, 2023
Audit
Dependencies
cramjamoptionalCompression for .apkg (zip replacement). Included automatically.