I can't make my add-on to be configurable from the add-on manager although I've followed the manual's directions

I followed the instructions from the manual but the “Config” button still remains disabled when I select my add-on in the add-on manager.

My addon’s folder contains the following files:

  • __init __.py
  • add-on.py
  • config.json
  • config.md
    And a user_files folder.

The config.json file contains a dictionary with two entries.

The add-on.py file begins with:

from aqt import editor, gui_hooks, mw
from aqt.addcards import AddCards
from aqt.utils import *
from aqt.forms import addcards

from datetime import datetime, timedelta
from time import *
import math
import sqlite3
import sys
import pathlib
import json

config = mw.addonManager.getConfig(__name__)

How to solve it?

Edit: I tried to implement the example provided in the manual but Anki returns the following error message when I try to open it:

An add-on you installed failed to load. If problems persist, please go to the Tools>Add-ons menu, and disable or delete the add-on.

When loading ‘⁨addOn⁩FromTheManual’:
⁨Traceback (most recent call last):
File “aqt.addons”, line 244, in loadAddons
File “C:\Users\User\AppData\Roaming\Anki2\addons21\addOn⁩FromTheManual\ __init __.py”, line 1, in
from . import addOn⁩FromTheManual
File “C:\Users\User\AppData\Roaming\Anki2\addons21\addOn⁩FromTheManual\addOn⁩FromTheManual.py”, line 3, in
print(“var is”, config[‘myvar’])
TypeError: ‘NoneType’ object is not subscriptable

P.s. I’m using Anki 2.1.53, Python 3.9.6, Windows 10 and Visual Studio Code 1.68.1.

1 Like

Works for me. The problem is most likely caused by a malformed config.json. getConfig() in this case returns None. Check your JSON or post the file here.

1 Like

Hint: check for those pesky trailing commas in your config. JSON doesn’t allow for trailing commas like many programming languages.

1 Like

Here’s the JSON code:

{

    "minutes": 40,
    "bars": 2
  
}

Looks correct, and I can’t reproduce your issue. The config button works for me with this config. If you can post the full code of the add-on, maybe that will help reveal the issue.

Sure. I’m making the code looks more neat right now (deleting some functions and variables that doesn’t have use anymore and translating the ones I didn’t write in English) and I’ll share it soon.

Meanwhile, here’s the code I used to reproduce the example provided in the add-on docs:

P.s. This add-on also didn’t have the “Config” button enabled and raised the error I described in the original post. The same error was raised when I tried to run the add-on I’m working on.

__init __.py

from . import addOn

addOn.py

from aqt import mw
config = mw.addonManager.getConfig(__name__)
print("var is", config['myvar'])

config.json

{"myvar": 5}

config.md

This is documentation for this add-on's configuration, in *markdown* format.

meta.json

{"name": "addOn", "mod": 0, "min_point_version": 0, "max_point_version": 53, "branch_index": 0, "config": {"myvar": 5}, "disabled": false, "conflicts": [], "update_enabled": false}

Also works for me. The getConfig function returns None in mainly two cases:

  1. If config.json doesn’t exist.
  2. If the JSON is not valid.

So, I have no idea why it doesn’t work for you.

Sending a zip of the add-on files will be nice.

It just came to my mind that the file was saved with the wrong codification, then I applied the UTF-8 codification instead of UTF-8 with BOM and it worked!

By the way, thank you for spending your time to help me once again.

2 Likes

All right. The add-on is almost done. I just have to address some exceptions and finish the process of cleaning the code.

1 Like

Oh, this is a common source of errors that didn’t come to my mind! I recommend turning on UTF-8 support on Windows 10 to save yourself from headaches: Is it possible to set "locale" of a Windows application to UTF-8? - Super User

1 Like