No console output? (for add-on developing)

Apologies if I missed something basic (new developer here): I have setup PyCharm on Mac with Python 3.10 following the official guide. I have created a test addon file, added it to the addon folder and got it to work. I have now included a print('hello world') in my test function (see screenshot). However, I don’t get any output at all to my console when I activate the addon in the Anki app. Is there anything else in addition to running /Applications/Anki.app/Contents/MacOS/anki in the terminal that I need to do?

Maybe you’ll have more luck running Anki from Terminal.app instead of inside PyCharm’s built-in terminal?

Thanks Damien. Unfortunately still nothing showing up in my console (both PyCharm’s built-in terminal and mac’s terminal.app). No idea what I am doing wrong here…

Next step in debugging would probably be trying to save to a file instead of printing (just to ensure you are reaching that line), with

with open("easy/to/reach/path", "w") as f:
    f.write("Hello, world!")

If that works, but not the print version, then I would check if the output is not (for some reasons) redirected somewhere (I assume Anki wouldn’t redirect its output, but who knows…), looking at the result of os.readlink(f"/proc/self/fd/{sys.stdout.fileno()}") (I’m not sure whether this works on MacOS, but it should since OS X is unix-compliant). It should be the same as if you opened a new python instance and ran that command (in the case it’s not redirected).

1 Like

Thanks BlackBeans! So: I managed to save to a file (as you suggested) - meaning I do reach that line of code and it is executed correctly, however I am having issues running os.readlink(f"/proc/self/fd/{sys.stdout.fileno()}") as I am getting different errors.

Correct me if I am wrong, but it seems like in my case Anki is redirecting its output somewhere else (i.e. not to stdout = terminal, correct?). Isn’t there a simple terminal command, which would redirect Anki’s output back to stdout?

Usually, when you redirect output, you don’t say output doesn’t go to stdout anymore: stdout is, by definition, where output goes. Instead, you say stdout = where I want my output to go from now one and you can’t restore it unless you kept a “backup” of it beforehand.

1 Like

That’s odd. Can you attach a screenshot showing you running Anki from Terminal.app with no output appearing? You should be seeing text like “Anki starting…” and so on.

Ah maybe we are getting somewhere now. I do not see “Anki starting…” anywhere in my terminal. All I do is running /Applications/Anki.app/Contents/MacOS/ankiin my terminal, which brings me to the screenshot I posted at the top. I then did all the testing.

Perhaps Anki isn’t really “running in my terminal”. I probably miss something very basic here. What exactly do I have to do - besides running the line above in my terminal - to see “Anki starting…” and so on?

Sorry for taking up your time with this one, guys, but I somehow need to get started…

Please make sure /Applications/Anki.app is the latest version of Anki. Typing that command in and pressing enter should be sufficient - could you include a screenshot of what you see after pressing enter?

This is what I see:

I am on version ⁨2.1.49 (dc80804a)⁩
Python 3.8.6 Qt 5.14.2 PyQt 5.14.2

I wanted to upgrade to the latest version a little bit later as some of my other add-ons may break.

use “showInfo”:

from aqt.utils import showInfo

output = ["exmpale", "output", "console"]

def example_console():
    showInfo(str(output))

menu = mw.form.menuCol
menu.addSeparator()
a = menu.addAction('console')
a.triggered.connect(example_console)

result

1 Like

Isn’t 2.1.49 executable file in /Contents/MacOS/AnkiMac? Not /Contents/MacOS/anki (Which is 2.1.50+)

print() works fine on my Mac when I run Anki on terminal. So it’s probably not an ‘Anki’ problem, and some other configuration issue.

4 Likes

100 points for BlueGreenMagick! You were exactly right. Everything is working now…