Page 1 of 1

reloading custom package

Posted: Fri Jun 22, 2018 10:41 pm
by malt
Is it possible to reload custom package? I can't seem to figure out a way to make changes in the custom package reflect in reloaded plugins. A server reboot works, but I'm wondering if theres a more obvious, better approach that I'm missing. Thanks.

Re: reloading custom package

Posted: Fri Jun 22, 2018 11:46 pm
by quartata
You mean programmatically?

Syntax: Select all

import importlib

...

importlib.reload(<module>)

Re: reloading custom package

Posted: Sat Jun 23, 2018 12:23 am
by malt
I edited my post a bit to clear up ambiguity.
Thanks for the response. Would that work? - since sp seems to cache the version of the package that existed on in initial load. ie, if the package prints a message when you sp plugin load a plugin that imports it, and you delete the package, it'll print the message instead of ModuleNotFoundError

Re: reloading custom package

Posted: Sat Jun 23, 2018 6:48 am
by Ayuto
Yes, it should work. Can you show us your test code?

Re: reloading custom package

Posted: Sun Jun 24, 2018 7:32 am
by malt
In plugins/testplugin/testplugin.py

Syntax: Select all

import testlib


in packages/custom/testlib/__init__.py

Syntax: Select all

print("newest testlib imported")
#print("even newer testlib imported")


So when I initially enter sp plugin load testplugin, it loads successfully and prints "newest testlib imported". When I reload testplugin, the message no longer prints. This holds true even when I replace the testlib/__init__.py file. I'm guessing this is intended behavior, but is there a way around it? Sorry if this is really obvious.

Re: reloading custom package

Posted: Sun Jun 24, 2018 8:55 am
by Ayuto
Like quartata mentioned reloading works fine:

Syntax: Select all

import testlib

import importlib
importlib.reload(testlib)
The caching is done by Python itself via a dictionary (sys.modules).

If you delete the module and use the above code, you will get an error like this:

Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File "..\addons\source-python\packages\source-python\plugins\command.py", line 162, in load_plugin
    plugin = self.manager.load(plugin_name)
  File "..\addons\source-python\packages\source-python\plugins\manager.py", line 193, in load
    plugin._load()
  File "..\addons\source-python\packages\source-python\plugins\instance.py", line 74, in _load
    self.module = import_module(self.import_name)

AttributeError: 'NoneType' object has no attribute 'name'