ImportErrors inside plugins are currently ignored → add logging #7396

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

Originally created by @9er on GitHub (Dec 20, 2022).

Originally assigned to: @abhi1693 on GitHub.

NetBox version

v3.4.0

Feature type

Change to existing functionality

Proposed functionality

This has been requested before, see #4628. I'm currently writing my first plugin and just spent about a day trying to figure out why it won't get loaded. No matter how far I turned up the log level, this pass in extras/plugins/urls.py always caught the ImportError inside my plugin:


    # Check if the plugin specifies any base URLs
    try:
        urlpatterns = import_string(f"{plugin_path}.urls.urlpatterns")
        plugin_patterns.append(
            path(f"{base_url}/", include((urlpatterns, app.label)))
        )
    except ImportError:
        pass

I propose replacing the pass with a log message that says something like "could not load plugin base URL {plugin_path}.urls.urlpatterns" and also log a traceback.

Since this mechanism is used to allow having plugins without API URLs (or base URLs), this is not neccessarily an error and should probably be log level INFO and the traceback should be DEBUG, I assume?

I have a patch ready, and can submit a PR, if this seems like a good idea. Log would look something like this:

gunicorn[1337]: Plugin base URL patterns could not be loaded: my_little_plugin.urls.urlpatterns

gunicorn[1337]: Traceback (most recent call last):
gunicorn[1337]: File "/opt/netbox/netbox/extras/plugins/urls.py", line 33, in <module>
gunicorn[1337]: urlpatterns = import_string(f"{plugin_path}.urls.urlpatterns")
gunicorn[1337]: File "/opt/netbox-3.4.0/venv/lib64/python3.8/site-packages/my_little_plugin/forms.py", line 2, in <module>
[...]
gunicorn[1337]: from dcim.models import Devic, Interface
gunicorn[1337]: ImportError: cannot import name 'Devic' from 'dcim.models' (/opt/netbox/netbox/dcim/models/__init__.py)

Use case

I hope this saves plugin developers some head-scratching.

Database changes

No response

External dependencies

No response

Originally created by @9er on GitHub (Dec 20, 2022). Originally assigned to: @abhi1693 on GitHub. ### NetBox version v3.4.0 ### Feature type Change to existing functionality ### Proposed functionality This has been requested before, see #4628. I'm currently writing my first plugin and just spent about a day trying to figure out why it won't get loaded. No matter how far I turned up the log level, this `pass` in `extras/plugins/urls.py` always caught the `ImportError` inside my plugin: ```python # Check if the plugin specifies any base URLs try: urlpatterns = import_string(f"{plugin_path}.urls.urlpatterns") plugin_patterns.append( path(f"{base_url}/", include((urlpatterns, app.label))) ) except ImportError: pass ``` I propose replacing the `pass` with a log message that says something like "could not load plugin base URL {plugin_path}.urls.urlpatterns" and also log a traceback. Since this mechanism is used to allow having plugins without API URLs (or base URLs), this is not neccessarily an error and should probably be log level `INFO` and the traceback should be `DEBUG`, I assume? I have a patch ready, and can submit a PR, if this seems like a good idea. Log would look something like this: ``` gunicorn[1337]: Plugin base URL patterns could not be loaded: my_little_plugin.urls.urlpatterns gunicorn[1337]: Traceback (most recent call last): gunicorn[1337]: File "/opt/netbox/netbox/extras/plugins/urls.py", line 33, in <module> gunicorn[1337]: urlpatterns = import_string(f"{plugin_path}.urls.urlpatterns") gunicorn[1337]: File "/opt/netbox-3.4.0/venv/lib64/python3.8/site-packages/my_little_plugin/forms.py", line 2, in <module> [...] gunicorn[1337]: from dcim.models import Devic, Interface gunicorn[1337]: ImportError: cannot import name 'Devic' from 'dcim.models' (/opt/netbox/netbox/dcim/models/__init__.py) ``` ### Use case I hope this saves plugin developers some head-scratching. ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: feature labels 2025-12-29 20:22:56 +01:00
adam closed this issue 2025-12-29 20:22:56 +01:00
Author
Owner

@abhi1693 commented on GitHub (Jan 13, 2023):

@jeremystretch I'd like to take this

@abhi1693 commented on GitHub (Jan 13, 2023): @jeremystretch I'd like to take this
Author
Owner

@kkthxbye-code commented on GitHub (Jan 13, 2023):

@abhi1693 all yours.

@kkthxbye-code commented on GitHub (Jan 13, 2023): @abhi1693 all yours.
Author
Owner

@9er commented on GitHub (Jan 14, 2023):

I have a patch ready, and can submit a PR, if this seems like a good idea.

I had pretty much the same thing ready, just didn't want to submit a PR without asking first, to follow the guidelines. Anyhow, thanks for fixing!

@9er commented on GitHub (Jan 14, 2023): > I have a patch ready, and can submit a PR, if this seems like a good idea. I had pretty much the same thing ready, just didn't want to submit a PR without asking first, to follow the guidelines. Anyhow, thanks for fixing!
Author
Owner

@jeremystretch commented on GitHub (Jan 23, 2023):

Per my comment here, it seems we just need to be a bit smarter about checking for the presence of optional modules. I've submitted PR #11566 to solve this for URL patterns specifically, but we should also consider the other optional modules a plugin may provide. (Importing these is handled inside PluginConfig.)

@jeremystretch commented on GitHub (Jan 23, 2023): Per my comment [here](https://github.com/netbox-community/netbox/pull/11499#issuecomment-1386364952), it seems we just need to be a bit smarter about checking for the presence of optional modules. I've submitted PR #11566 to solve this for URL patterns specifically, but we should also consider the other optional modules a plugin may provide. (Importing these is handled inside `PluginConfig`.)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7396