Anki setup in Pycharm

I’d greatly appreciate if any Anki developers out there could provide me with some guidance on how to set up Pycharm for Anki development.

I previously had a setup for my add-on development with anki attached in Pycharm to run in debug mode. However, since the switch to the bazel build system, I unfortunately haven’t managed to get things working. I’m using a Mac, fyi.

For starters, to keep it simple without an add-on, how would you run Anki alone within Pycharm? The steps I have followed so far are as follows:

  1. Build anki from the terminal using: ./run
  2. Open the anki folder in Pycharm
  3. Create a new virtual environment with python 3.8
  4. In the terminal, activate the venv and run pip install -r pip/requirements.txt and pip install pyqt5 pyqtwebengine

After that is where I keep running into problems. If I create a run configuration with ‘qt/runanki.py’ as the script path and try running, I get an error “ModuleNotFoundError: No module named ‘aqt’”

In Preferences|Project|Project Structure, I tried setting ‘qt/aqt’ and ‘pylib/anki’ as Sources, but running then gave me the error “ImportError: cannot import name ‘MethodType’ from partially initialized module ‘types’ (most likely due to a circular import)” from aqt/init.py

Any help would be greatly appreciated! :slight_smile:

When you say “within Pycharm”, I presume you’re trying to take advantage of the debugging features. That is not something I’ve tried, as I run Anki outside of the editor and rely on print statements when necessary.

If your goal is add-on development, you can install the aqt package from pip and don’t even need Anki’s sources. If you’d like to contribute to Anki’s code, you may be able to get debugging working by building wheels first - it’s covered in development.md

Thanks for the reply, Damien. If I install aqt or build and installs wheels into an addon project venv, would there be a way to start Anki from Pycharm this way? What I would really like is to set breakpoints in my own add-on code and also in Anki’s code. My goal is to catch my own bugs and also better understand the Anki code base.

With previous versions of Anki, I was able to achieve this workflow in this way: Attaching debugger in PyCharm for addon development

@mcs, if you configure the project interpreter in PyCharm with the virtual env in which you’ve installed aqt, etc, and you also install, pytest-anki into that same environemtn, then you can write tests that use pytest anki’s pytest fixtures, which handle starting Anki for you and attaching to the appropriate python process. This allows debugging add-ons and stepping through Anki’s source code pathways that your add-on calls. In this case, there is no need to know/care about bazel or building from source. However, this workflow isn’t great for developing Anki itself. That would require some additional steps for a better experience.

@andrewsanchez Thanks very much! I’ll give that a try :slight_smile:

As an aside, my ultimate goal is to contribute to the development of Anki. While I am relatively proficient in Python and have perused a fair bit of the Anki code base now, I am still very much lacking the knowledge of how to best set up a development environment/workflow. Any tips or guidance would be greatly appreciated!

A Dockerfile or docker compose set up would be ideal, IMO. One thing I’ve been meaning to look into is whether bazel is capable of generating an image. If so, it would be trivial to get up and running with an Anki dev env with all of PyCharm’s bells and whistles. I haven’t been able to build Anki from source since it moved to bazel. Admittedly, I haven’t spent a lot of time debugging the issues I ran into.

If the instructions in docs/ don’t work for you, please create a thread about it :slight_smile:

This explains a lot about the Anki source code. Sorry don’t mean to sound condescending, but I could not build Anki from the source code with Bazel either.

I would love a docker-compose setup aswell, preferably with a volume mapping to the source code, so a tool like nodemon can watch for changes and automatically rebuild and restart the app.

From www jetbrains com /help/pycharm/remote-debugging-with-product.html#remote-debug-config:

#==============this code added==================================================================:
import sys
sys.path.append("<PyCharm directory>/debug-egg/pydevd-pycharm.egg")
import pydevd_pycharm

pydevd_pycharm.settrace('172.20.208.95', port=12345, stdoutToServer=True,
                        stderrToServer=True)
#================================================================================================

I added this to qt/runanki.py in an appropriate place. According to https youtrack jetbrains com/issue/PY-43558 it is required to extract the files stored in pydevd-pycharm.egg in the debug-eggs directory of pycharm. It’s a zipfile so its easy to extract, I put it another location and updated the path accordingly.

Then I followed the instructions at https www jetbrains com/help/pycharm/run-debug-configuration-python-remote-debug.html to create a debug configuration. Run the debug configuration prior to building and running anki. I was then able to run AnkiDesktop in PyCharm.

Asking it to “download the source code” from the debug server seems to allow for source code stepping, but that’s not an ideal configuration. I’ll continue to make efforts in this regard.

Cheers

On Windows, PyCharm is unable to handle the “symbolic links” that are used to access the source files from the “run” directory. Can this be disabled? Can I run Anki from another directory? Since the only instructions I have for running an Anki build are: “.\run” and it immediately calls Bazel which seems to be a labyrinthine monolith.

I am completely at a loss. The complexity of Bazel is such that it will take me hours to tease this apart. Can anyone help?

Update

I used 7-Zip to create an archive of the files in:

C:\bazel\anki\hgbcl6f6\execroot\ankidesktop\bazel-out\x64_windows-fastbuild\bin\qt\runanki.exe.runfiles

In the hopes that extracting the archive to a new directory would help somehow. Sadly, when I try “runanki.exe” it just says it can’t find the runfiles directory. I’m a bit weary of banging my head against this brick wall.

Any suggestions?

Thanks in advance

It sounds like you are attempting to launch Anki in a debugger. That was not what ./run was intended for, and as I mentioned above, you may have more luck using the wheels in that case. I have expanded on it a bit in the docs: anki/development.md at main · ankitects/anki · GitHub