I think using AnkiConnect
would be your best bet, but if you understand this section of the Anki add-on writing guide, I think you can also easily achieve your goal by using importlib
and Anki’s debug console as @Rumo said.
Here is an example of how to do it:
Library:
-
Download the library to an arbitrary location using pip install --target.
(I think you should use the same version of Python that Anki is using).pip install --target=c:\tmp-py-lib eng-to-ipa
Deck
- Deck name: phonetic test
Note type
- Fields
- English
- e.g. apple, ball, cat, dog, egg
- IPA
- Back
- English
Debug console
- Start Anki and open the debug console.
- Enter the following code at the upper part of the console, then run it with Ctrl + Enter ( Command + Return on a Mac):
import importlib.util
import sys
# https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
lib_path = r"C:\tmp-py-lib\eng_to_ipa\__init__.py"
spec = importlib.util.spec_from_file_location("eng_to_ipa", lib_path)
module = importlib.util.module_from_spec(spec)
sys.modules["eng_to_ipa"] = module
spec.loader.exec_module(module)
import eng_to_ipa as ipa
updated_notes = []
# Iterate over all notes in a deck
for note_id in mw.col.find_notes('"deck:phonetic test"'):
note = mw.col.get_note(note_id)
if not note["IPA"]:
note["IPA"] = ipa.convert(note["English"])
updated_notes.append(note)
if updated_notes:
mw.col.update_notes(updated_notes)