# PyCharm setup for add-on debugging

**URL:** <https://forums.ankiweb.net/t/pycharm-setup-for-add-on-debugging/17733>\
**Category:** Development\
**Created:** [February 21, 2022, 5:27am UTC](https://forums.ankiweb.net/t/pycharm-setup-for-add-on-debugging/17733 "2022-02-21T05:27:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcs](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mcs](https://forums.ankiweb.net/u/mcs)\
**Post date:** [February 21, 2022, 5:27am UTC](https://forums.ankiweb.net/t/pycharm-setup-for-add-on-debugging/17733/1 "2022-02-21T05:27:08Z")

</div>

I thought I would share my current workflow for add-on development in PyCharm. It admittedly feels like a bit of a ‘hack’, so I would warmly welcome any comments/criticisms from more experienced developers. But hopefully it will help a few people out.

My main goals were as follows:

- Be able to launch Anki from within PyCharm
- Run in debug mode so I could see `print` statement outputs and place breakpoints in both my own add-on code and the Anki source code (eg. to see within Anki how variables are set, the flow between functions, etc.)

I had a lot of head-scratching to get a process working since the switch to the Bazel build system. I still have not been able to actually launch Anki within PyCharm from the downloaded source code, only from pre-built wheels as shown below.

## Project structure

This isn’t strictly necessary, but I use the following project structure prescribed by **anki-addon-builder** :[anki-addon-builder](https://github.com/glutanimate/anki-addon-builder)

```auto
{my_addon_name}
└── runanki.py (copied from anki source; used to run in Pycharm)
└── addon.json [required only for aab]
└── src
    └── {my_addon_name}
 			   ├── project files ( __init__.py, etc.)

```

#### runanki.py

A key here is the `runanki.py` file. This is what allows Anki to be launched from within the PyCharm project. I copied the file directly from the Anki source code, but its contents are as follows:

```python
#!/usr/bin/env python3
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

import os
import sys

try:
    import bazelfixes

    bazelfixes.fix_pywin32_in_bazel()
    bazelfixes.fix_extraneous_path_in_bazel()
except ImportError:
    pass

import aqt

if not os.environ.get("ANKI_IMPORT_ONLY"):
    aqt.run()

```

## Virtual environment setup

Create a virtual environment and install **anki** and **aqt**. You can either build wheels from the source code (as detailed in the documentation) or download the current versions from PyPI. This can be done on the command line, but a simpler method within PyCharm is detailed below.

Open up the root folder of the project in PyCharm. Open the PyCharm Preferences dialog and select Project \> Python Interpreter on the sidebar. In the ‘Python Interpreter’ dropdown, select ‘Show All…’, then click the ‘Add’ button to create a new virtual environment.

 ![image-20220220124215956](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/d/dc90c5b106e74f2fd2dba2527aa0e4023522d0a9.jpeg)  
 ![image-20220220124346728](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/f/f82ba2d79f9be7261b4c0c7a5d5fe121032782c2.png)

Insert any desired location for the virtual environment and select the Python interpreter (ideally, it should match the Python version used in the latest Anki build).

 ![image-20220220124527491](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/f/fe3310b2a2c89c17c15295dc0940b5f1853eeefd.jpeg)

Now, click the ‘+’ button to install the **aqt** and **anki** packages.

 ![image-20220220124735625](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/a/a15d298e6743aa035527a61df5e49f24668b8b48.png)  
 ![image-20220220124850125](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/8/81a1f903706f038f42db01b68d8c97ea91a3373a.png)

## Run configuration

Add a new run/debug configuration. For the **Script Path** , select the `runanki.py` file in the project directory. **Python Interpreter** should be Python in the project’s virtual environment.

If you want to automatically load a certain Anki profile (eg. a separate one for development), you can add `-p Development_profile_name` to the **Parameters** field.

 ![image-20220220224400947](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/4/47144605ae46dd933e05d08937954d1295de0354.png)  
 ![image-20220220224738629](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/7/7aabf53626c74bac435e33b585ef2529d38fca5c.png)  
 ![image-20220220225222020](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/b/b6a9746782dd96fb52cbe9daa7d94303ce01c418.png)

## Running Anki in debug mode

If you click the debug (or run) button in the upper right of the window, it should now launch Anki. The add-on itself won’t be available to Anki yet, but we’ll fix that shortly (see below).

You can find the anki source files in the packages under **External Libraries** in the sidebar. You can place breakpoints in any file to pause execution (eg. to examine the values of variables or to step through the code execution).

 ![image-20220220231627255](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/8/8f3ec9030f2cc35c8cc42985b88ca3b9f2b03232.png)  
 ![image-20220220234017571](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/b/bc1cf41adb6a9c0fd7b4c3e83cdc02cef0a69c0f.png)

## Activating the add-on

The add-on directory needs to be within the Anki add-ons directory in order for it to be loaded. This can be done by creating an alias. On the Mac, a regular alias won’t work and you have to create a symlink using the Terminal as follows:

```bash
ln -s "/{PATH TO PROJECT DIR}/my_addon/src/my_addon" \
"/{PATH TO ANKI ADDONS}/addons21/{MY ADDON NAME}"

```

Note that **only** the `my_addon` directory within the `src` directory is symlinked, not the root project directory. Replace the curly bracket components, and whatever the symlink folder is named will be displayed as the add-on name in Anki.

After launching Anki, check to make sure that the add-on is active using the usual Add-ons menu item.

Now, you can add breakpoints to your add-on code and see `print` statements in the console.

 ![image-20220221000916995](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/5/50d0bc88423ff9e5f149884cc1711adb563774f1.png)

## Suppressing the Import window

For some reason, when you launch Anki in debug mode (not run mode), it will always show the **Import** window on startup.

 ![image-20220220231457028](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/5/5f395f9949688b39a396cb4fb8c07ee51316b0e7.png)

You can suppress this by making a change to the aqt/main.py file. Of course, you shouldn’t really edit files in the libraries in this way (and PyCharm will show a warning), so look at this as a bit of a hack.

In the file aqt/main.py, find the function `def onAppMsg(self, buf: str)`. Near the end of the function, you will see the following:

```python
# import / add-on installation
if is_addon:
    self.installAddon(buf)
else:
    self.handleImport(buf)

```

Comment out the line `self.handleImport(buf)` and replace it with `pass`:

```python
# import / add-on installation
if is_addon:
    self.installAddon(buf)
else:
    # self.handleImport(buf)
    pass

```

This should stop the **Import** window from displaying on startup.

---

<div class="post-metadata">

**Author:** ![Ambriel](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/ambriel/32/11108_2.png) [@Ambriel](https://forums.ankiweb.net/u/Ambriel)\
**Post date:** [October 5, 2022, 8:53pm UTC](https://forums.ankiweb.net/t/pycharm-setup-for-add-on-debugging/17733/2 "2022-10-05T20:53:30Z")

</div>

Thank you for this contribution.

I have used this gracious resource to help create a tutorial on YouTube explaining this process.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 1, 2023, 3:14am UTC](https://forums.ankiweb.net/t/pycharm-setup-for-add-on-debugging/17733/3 "2023-05-01T03:14:31Z")

</div>

Closed on May 1, 2023, 3:14am UTC


