Files
anki/docs-site/addons/debugging.mdx
Luc Mcgrady a6b2457a42 docs: use relative links (#5048)
<!--
Title (for the Pull Request title field at the top):
Use a short prefix so the change type is obvious. You do not need to
repeat it in the body below.

Examples:
- fix: — bugfix
- feat: — feature
- refactor: — internal change without user-facing feature
- docs: — documentation only
- chore: — tooling, CI, deps, build housekeeping
- test: — tests only
-->

## Linked issue (required)

closes #5011


All the "apps.ankiweb.net" Are broken in the form that they currently
exist which makes it difficult to confirm my changes would link to the
correct files so I've left them as they are in this pr.

---------

Co-authored-by: Abdo <abdo@abdnh.net>
2026-06-22 13:18:45 +03:00

103 lines
3.0 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Debugging"
---
## Exceptions and Stdout/Stderr
If your code throws an uncaught exception, it will be caught by Ankis standard
exception handler, and an error will be presented to the user.
The handler catches anything that is printed to stderr, so you should avoid logging text
to stderr unless you want the user to see it in a popup.
Text printed to standard output is covered in [this section](./console-output).
## Webviews
If you set the env var QTWEBENGINE_REMOTE_DEBUGGING to 8080 prior to starting Anki,
you can surf to http://localhost:8080 in Chrome to debug the visible webpages.
Alternatively, you can use [this add-on](https://ankiweb.net/shared/info/31746032)
to open the inspector inside Anki.
## Debug Console
Anki also includes a REPL. From within the program, press the [shortcut
key](/manual/misc#debug-console) and a
window will open up. You can enter expressions or statements into the
top area, and then press ctrl+return/command+return to evaluate them. An
example session follows:
```text
>>> mw
<no output>
>>> print(mw)
<aqt.main.AnkiQt object at 0x10c0ddc20>
>>> invalidName
Traceback (most recent call last):
File "/Users/dae/Lib/anki/qt/aqt/main.py", line 933, in onDebugRet
exec text
File "<string>", line 1, in <module>
NameError: name 'invalidName' is not defined
>>> a = [a for a in dir(mw.form) if a.startswith("action")]
... print(a)
... print()
... pp(a)
['actionAbout', 'actionCheckMediaDatabase', ...]
['actionAbout',
'actionCheckMediaDatabase',
'actionDocumentation',
'actionDonate',
...]
>>> pp(mw.reviewer.card)
<anki.cards.Card object at 0x112181150>
>>> pp(card()) # shortcut for mw.reviewer.card.__dict__
{'_note': <anki.notes.Note object at 0x11221da90>,
'_qa': [...]
'col': <anki.collection._Collection object at 0x1122415d0>,
'data': u'',
'did': 1,
'due': -1,
'factor': 2350,
'flags': 0,
'id': 1307820012852L,
[...]
}
>>> pp(bcard()) # shortcut for selected card in browser
<as above>
```
Note that you need to explicitly print an expression in order to see
what it evaluates to. Anki exports pp() (pretty print) in the scope to
make it easier to quickly dump the details of objects, and the shortcut
ctrl+shift+return will wrap the current text in the upper area with pp()
and execute the result.
## PDB
If youre on Linux or are running Anki from source, its also possible
to debug your script with pdb. Place the following line somewhere in
your code, and when Anki reaches that point it will kick into the
debugger in the terminal:
```python
from aqt.qt import debug; debug()
```
Alternatively you can export DEBUG=1 in your shell and it will kick into
the debugger on an uncaught exception.
## Python Assertions
Runtime checks using Python's `assert` statement are not evaluated in
Anki's release builds, even when running in debug mode. If you want to
use `assert` for testing you can use the [packaged versions from PyPI](https://betas.ankiweb.net/#via-pypipip)
or [run Anki from source](https://github.com/ankitects/anki/blob/main/docs/development).