Log in

View Full Version : Reload modules on preview.


Mystery Keeper
25th October 2016, 20:33
Some of you might have faced the problem of editing .py modules while working with VapourSynth Editor. Modules are not reloaded on refresh. Well, one of users has found a cure!
import sys
modules_to_reload = ['havsfunc', 'MCDenoise', 'helpers']
for module in modules_to_reload:
if module in sys.modules:
del sys.modules[module]
List the names of the modules you import - and they'll be properly reloaded when you refresh the preview. You can add it to the new script template. My template looks like this:
from __future__ import print_function

import vapoursynth as vs
core = vs.get_core(threads=8)
core.set_max_cache_size(12000)

import sys
sys.path.append('D:\\vapoursynth-plugins\\py\\')
import platform
architecture = platform.architecture()
if architecture[0] == '64bit':
vapoursynth_plugins_path = 'D:\\vapoursynth-plugins\\64bit\\'
else:
vapoursynth_plugins_path = 'D:\\vapoursynth-plugins\\32bit\\'
print('Plugins folder: ', vapoursynth_plugins_path, end='\n', file=sys.stderr)
import os
for filename in os.listdir(vapoursynth_plugins_path):
if filename[-4:] != '.dll':
continue
try:
core.std.LoadPlugin(vapoursynth_plugins_path + filename)
except Exception as e:
print('Error: ', e, end='\n', file=sys.stderr)

modules_to_reload = ['havsfunc', 'MCDenoise', 'helpers']
for module in modules_to_reload:
if module in sys.modules:
del sys.modules[module]

splinter98
26th October 2016, 00:31
A bit of Python History:

Python 2 used to have a reload builtin (which effectively does the same as the code above, but followed by an importing it) to allow you to reload modules, however this had a lot of issues as it only reloads that module so if the module imports another module this doesn't get reloaded either which can cause issues with multiple instances of modules being used at the same time.

You can still reload a module in Python 3.4 however using the following function: https://docs.python.org/3/library/importlib.html#importlib.reload or in 3.2+ (deprecated since 3.4 when importlib replaced imp) https://docs.python.org/3/library/imp.html#imp.reload

Also this might be good reference: http://pyunit.sourceforge.net/notes/reloading.html it proposes a better solution but I haven't tested it and it doesn't use the imp or importlib module so it may still fail in certain edge cases.