# Importing deck using python

**URL:** <https://forums.ankiweb.net/t/importing-deck-using-python/21236>\
**Category:** Add-ons\
**Created:** [July 6, 2022, 11:47am UTC](https://forums.ankiweb.net/t/importing-deck-using-python/21236 "2022-07-06T11:47:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jr\_123](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@Jr\_123](https://forums.ankiweb.net/u/Jr_123)\
**Post date:** [July 6, 2022, 11:47am UTC](https://forums.ankiweb.net/t/importing-deck-using-python/21236/1 "2022-07-06T11:47:59Z")

</div>

I am working on an addon, for anki, which requires me to import a new deck using its python API. I found the function called test\_apkg ([anki/test\_importing.py at 627f910635893f8205f7004572ba2ac7f3d987b8 · ankitects/anki · GitHub](https://github.com/ankitects/anki/blob/627f910635893f8205f7004572ba2ac7f3d987b8/pylib/tests/test_importing.py))  
which seems to have parts which do that. I tried implementing that below, I am getting no errors, however the deck does not get imported. How do I make my testFunction() import a deck for anki?

```auto
from aqt import mw
from aqt.utils import showInfo, qconnect
from aqt.qt import *

from anki.importing import *

import os

from anki.collection import Collection as aopen

#from anki.pylib.tests.shared import getEmptyCol

from .shared import getEmptyCol

    
def testFunction() -> None:

    file = "flashcards.apkg"

    col = getEmptyCol()

    apkg = r"path_to_file/flashcards.apkg"
    imp = AnkiPackageImporter(col, apkg)

    imp.run()

# create a new menu item, "test"
action = QAction("Import", mw)
# set it to call testFunction when it's clicked
qconnect(action.triggered, testFunction)
# and add it to the tools menu
mw.form.menuTools.addAction(action)

```

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [July 8, 2022, 12:25am UTC](https://forums.ankiweb.net/t/importing-deck-using-python/21236/2 "2022-07-08T00:25:34Z")

</div>

Maybe imp.log will reveal something.
