Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
XioNoX opened this issue Jun 24, 2024 · 0 comments
Open

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

XioNoX opened this issue Jun 24, 2024 · 0 comments
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@XioNoX
Copy link
Contributor

XioNoX commented Jun 24, 2024

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

@XioNoX XioNoX added status: needs triage This issue is awaiting triage by a maintainer type: bug A confirmed report of unexpected behavior in the application labels Jun 24, 2024
@jeremystretch jeremystretch removed their assignment Jun 25, 2024
@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation severity: low Does not significantly disrupt application functionality, or a workaround is available and removed status: needs triage This issue is awaiting triage by a maintainer labels Jun 25, 2024
@jeremystretch jeremystretch added status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation and removed status: accepted This issue has been accepted for implementation labels Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation type: bug A confirmed report of unexpected behavior in the application
2 participants