Sublime Forum

Resolving Issue With load_settings Method

#1

I’ve got my <project_name>.sublime.settings file in the same directory as the plug-in itself. The JSON inside the settings file is good, in fact, there is just a single entry. The code is nothing more than:

settings = sublime.load_settings("<project_name>.sublime-settings")
print("settings: ", settings)
print(settings.get("check_value"))  # just a string

The settings object is valid as the print statement returns something like:

<sublime.Settings object at 0x000001DDBE6F0780>

but “check_value” always evaluates to None. No errors or warnings are thrown in the console.

There are no other settings files that might override the “check_value” entry.

Not sure what else to check. All I can think of is that the incorrect file is not being loaded or cannot be found for some reason. Is there a way to dig into the load_settings method to get a better idea as to what might be going wrong? Any other debugging strategies that I could employ to figure this out? Thanks again.

1 Like

#2

As far as I am aware, there’s no way to detect when you’re trying to load a settings file that does not exist short of manually writing code to detect if the file is there or not, which doesn’t help if you can’t tell which file Sublime is loading (or files, since it loads all matching files and collates them together).

In addition to not telling you if you try to load a settings file that does not exist, you also get no error message if you try to load a settings file that is not valid JSON (although after you’ve loaded it, breaking the syntax of the file and saving will give you an error dialog).

That said, this seems to work for me OK, so perhaps comparing what you’re doing to this will help? load_settings only accepts a filename but no path (which it doesn’t look like you’re doing) so the only thing that comes to mind is either a somehow broken file that’s stopping it from loading, or a typo in the filename.

0 Likes

#3

My settings are now working, but I’m not entirely sure how it happened. For kicks, I modified my settings file by eliminating the opening curly brace. When I ran the plug-in, Sublime (not my plug-in), displayed an error in a message box stating the JSON was invalid. I restored the missing curly brace. Upon saving the file, I noticed in the console Sublime had written:

reloading settings Packages/<folder>/<plug-in name>.sublime-settings

I do not recall seeing any sort of messages like this when saving a settings file previous to this. Now, with the real JSON in place, my code is working as it should.

I’m guessing Sublime is using some sort of caching strategy to preserve setting file information. And my file somehow got stuck in this cache (although I had restarted Sublime more than once throughout this issue). Only after receiving a hard error did Sublime try to refresh the cache. Note that this is pure speculation on my part, but I have to think the problem must be something close to this. At any rate, your post did get me thinking, and ultimately helped me figure out this issue. Thanks again.

1 Like

#4

I guess the problem can be you’re not saving the .sublime-settings file out of the Package\User folder. Some time ago I had a problem similar and I solved let the file in the user folder.

settings = sublime.load_settings("project_name.sublime-settings")
print(settings.get('check_value'))
settings.set('check_value', 'some_string')
sublime.save_settings("project_name.sublime-settings")
# open again
settings = sublime.load_settings("project_name.sublime-settings")

0 Likes