Custom scripts in sub packages should not be cached #7380

Closed
opened 2025-12-29 20:22:39 +01:00 by adam · 0 comments
Owner

Originally created by @kkthxbye-code on GitHub (Dec 17, 2022).

Originally assigned to: @kkthxbye-code on GitHub.

NetBox version

v3.4.1

Python version

3.10

Steps to Reproduce

Create the structure described in the following issue:

https://github.com/netbox-community/netbox/issues/10258

Change the Script class name or the variable name of any ScriptVariable.

Expected Behavior

The new code should be dynamically reloaded.

Observed Behavior

Code is not reloaded.

We have had discussions about this at the last maintainers meeting. Dynamically loading code in python is not a pleasent experience, and as such there is a bunch of pitfalls. Currently we delete the module entry in sys.modules when loading the script list, this prevent caching. This doesn't work for nested packages, which this issue seeks to address.

In addition to this, there's other issues that might be also be addressed:

  • The way we currently load scripts is to load any module/package in the SCRIPTS_ROOT folder in a way that makes the module path be in the root path. Essentially this means that if we load a file called ipam.py in the scripts folder, it will clash with the already existing ipam module.
  • load_module is to be deprecated in python 3.12, so we need to refactor the script/report loading code at some point soon regardless.

I'll be looking into solutions to these problems, but if anyone has experience with dynamically loading python code in a long running python process, please feel free to give input.

For reference, the loading code is here:

3675ad2539/netbox/extras/scripts.py (L513-L543)

Originally created by @kkthxbye-code on GitHub (Dec 17, 2022). Originally assigned to: @kkthxbye-code on GitHub. ### NetBox version v3.4.1 ### Python version 3.10 ### Steps to Reproduce Create the structure described in the following issue: https://github.com/netbox-community/netbox/issues/10258 Change the `Script` class name or the variable name of any `ScriptVariable`. ### Expected Behavior The new code should be dynamically reloaded. ### Observed Behavior Code is not reloaded. We have had discussions about this at the last maintainers meeting. Dynamically loading code in python is not a pleasent experience, and as such there is a bunch of pitfalls. Currently we delete the module entry in `sys.modules` when loading the script list, this prevent caching. This doesn't work for nested packages, which this issue seeks to address. In addition to this, there's other issues that might be also be addressed: * The way we currently load scripts is to load any module/package in the `SCRIPTS_ROOT` folder in a way that makes the module path be in the root path. Essentially this means that if we load a file called ipam.py in the scripts folder, it will clash with the already existing ipam module. * `load_module` is to be deprecated in python 3.12, so we need to refactor the script/report loading code at some point soon regardless. I'll be looking into solutions to these problems, but if anyone has experience with dynamically loading python code in a long running python process, please feel free to give input. For reference, the loading code is here: https://github.com/netbox-community/netbox/blob/3675ad2539400038af6797140ddca3679b8bca30/netbox/extras/scripts.py#L513-L543
adam added the type: bugstatus: accepted labels 2025-12-29 20:22:39 +01:00
adam closed this issue 2025-12-29 20:22:39 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7380