reloading custom package

Please post any questions about developing your plugin here. Please use the search function before posting!
malt
Junior Member
Posts: 4
Joined: Sat Feb 15, 2014 7:22 am

reloading custom package

Postby malt » Fri Jun 22, 2018 10:41 pm

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.
Last edited by malt on Fri Jun 22, 2018 11:58 pm, edited 1 time in total.
User avatar
quartata
Member
Posts: 77
Joined: Wed Jun 22, 2016 11:49 pm

Re: reloading custom package

Postby quartata » Fri Jun 22, 2018 11:46 pm

You mean programmatically?

Syntax: Select all

import importlib

...

importlib.reload(<module>)
malt
Junior Member
Posts: 4
Joined: Sat Feb 15, 2014 7:22 am

Re: reloading custom package

Postby malt » Sat Jun 23, 2018 12:23 am

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
User avatar
Ayuto
Project Leader
Posts: 1911
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: reloading custom package

Postby Ayuto » Sat Jun 23, 2018 6:48 am

Yes, it should work. Can you show us your test code?
malt
Junior Member
Posts: 4
Joined: Sat Feb 15, 2014 7:22 am

Re: reloading custom package

Postby malt » Sun Jun 24, 2018 7:32 am

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.
User avatar
Ayuto
Project Leader
Posts: 1911
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: reloading custom package

Postby Ayuto » Sun Jun 24, 2018 8:55 am

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'

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 8 guests