Scripts: can't import modules relative to script file #9887

Closed
opened 2025-12-29 21:24:01 +01:00 by adam · 2 comments
Owner

Originally created by @XioNoX on GitHub (Jun 24, 2024).

Originally assigned to: @arthanson on GitHub.

Deployment Type

Self-hosted

NetBox Version

v4.0.3

Python Version

3.11

Steps to Reproduce

  1. Set SCRIPT_ROOT to a directory outside Netbox's root
  2. Create/import a script that is meant to be imported by other scripts, for example _common.py which contains a class Importer (see this file for example)
  3. Create a regular script that imports the above mentioned file/class from _common import Importer (see this file for example)

Expected Behavior

Hi,

Not sure when that changed, but on 3.2.9, the imported module worked fine, this reduces duplicated code between scripts. That's why I'm opening it as bug report rather than feature request.

Observed Behavior

When opening the script's page, it shows the following error message:

Error loading script.
No module named '_common'

I've been thinking of possible workarounds, I'm obviously not knowledgeable enough on Netbox code, so not sure of all the tradeoff they brings.

  1. Add the following to the script being executed, tested and works as expected, but probably not optimal
import sys
from os.path import dirname
sys.path.append(dirname(__file__))
from _common import Importer
  1. In settings.py add sys.path.append(SCRIPTS_ROOT) but there is probably a risk of name collision between Netbox modules and scripts ?
  2. Add the scripts (or at least imported modules) inside Netbox's BASE_DIR (like the default /scripts. Here depending on Netbox deployment process, it might get wiped at each upgrade (like in our case).
  3. Same as (3), but using a symlink instead, so far my preferred workaround if there is no plans on changing this behavior upstream (and then do the import like from scripts._common import Importer as imports work from Netbox's root directory).

Let me know if I can be of any help.

Thanks

Originally created by @XioNoX on GitHub (Jun 24, 2024). Originally assigned to: @arthanson on GitHub. ### Deployment Type Self-hosted ### NetBox Version v4.0.3 ### Python Version 3.11 ### Steps to Reproduce 1. Set `SCRIPT_ROOT` to a directory outside Netbox's root 2. Create/import a script that is meant to be imported by other scripts, for example `_common.py` which contains a class `Importer` (see [this file](https://github.com/wikimedia/operations-software-netbox-extras/blob/76c69b2f324a4a269b991d5c3e43cc5f6bc43ac7/customscripts/_common.py) for example) 3. Create a regular script that imports the above mentioned file/class `from _common import Importer` (see [this file](https://github.com/wikimedia/operations-software-netbox-extras/blob/76c69b2f324a4a269b991d5c3e43cc5f6bc43ac7/customscripts/add_secondary_ips.py#L3) for example) ### Expected Behavior Hi, Not sure when that changed, but on 3.2.9, the imported module worked fine, this reduces duplicated code between scripts. That's why I'm opening it as bug report rather than feature request. ### Observed Behavior When opening the script's page, it shows the following error message: ``` Error loading script. No module named '_common' ``` I've been thinking of possible workarounds, I'm obviously not knowledgeable enough on Netbox code, so not sure of all the tradeoff they brings. 1. Add the following to the script being executed, tested and works as expected, but probably not optimal ``` import sys from os.path import dirname sys.path.append(dirname(__file__)) from _common import Importer ``` 2. In `settings.py` add `sys.path.append(SCRIPTS_ROOT)` but there is probably a risk of name collision between Netbox modules and scripts ? 3. Add the scripts (or at least imported modules) inside Netbox's `BASE_DIR` (like the default `/scripts`. Here depending on Netbox deployment process, it might get wiped at each upgrade (like in our case). 4. Same as (3), but using a symlink instead, so far my preferred workaround if there is no plans on changing this behavior upstream (and then do the import like `from scripts._common import Importer` as imports work from Netbox's root directory). Let me know if I can be of any help. Thanks
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 21:24:01 +01:00
adam closed this issue 2025-12-29 21:24:01 +01:00
Author
Owner

@arthanson commented on GitHub (Sep 11, 2024):

@XioNoX I think this isn't a bug, IMHO it could/should be handled on the server config by setting pythonpath (https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) in the environment if you want other directories to be in your python path outside of NetBox directory. See also: https://stackoverflow.com/questions/4757178/how-do-you-set-your-pythonpath-in-an-already-created-virtualenv for in a virtualenv

@arthanson commented on GitHub (Sep 11, 2024): @XioNoX I think this isn't a bug, IMHO it could/should be handled on the server config by setting pythonpath (https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) in the environment if you want other directories to be in your python path outside of NetBox directory. See also: https://stackoverflow.com/questions/4757178/how-do-you-set-your-pythonpath-in-an-already-created-virtualenv for in a virtualenv
Author
Owner

@arthanson commented on GitHub (Sep 13, 2024):

closing

@arthanson commented on GitHub (Sep 13, 2024): closing
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9887