amyasnikov / validity

NetBox plugin to validate network devices
MIT License
136 stars 8 forks source link

ImportError on running ./migrate.py on Netbox 4.0 #95

Closed lc787 closed 5 months ago

lc787 commented 5 months ago

As per the installation guide, it is required to create DB tables: ./manage.py migrate validity

Running this on Netbox 4.0 outputs the following

Traceback (most recent call last):
  File "/opt/netbox/netbox/./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 416, in execute
    django.setup()
  File "/opt/netbox/venv/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/opt/netbox/venv/lib/python3.12/site-packages/django/apps/registry.py", line 124, in populate
    app_config.ready()
  File "/opt/netbox/venv/lib/python3.12/site-packages/validity/__init__.py", line 36, in ready
    return super().ready()
           ^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/netbox/plugins/__init__.py", line 115, in ready
    if template_extensions := self._load_resource('template_extensions'):
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/netbox/plugins/__init__.py", line 91, in _load_resource
    module = import_module(default_module)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/netbox/.pyenv/versions/3.12.3/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/opt/netbox/venv/lib/python3.12/site-packages/validity/template_content.py", line 3, in <module>
    from extras.plugins import PluginTemplateExtension
ImportError: cannot import name 'PluginTemplateExtension' from 'extras.plugins' (unknown location)

After checking the plugin migration guide to 4.0, I've spotted that imports need to be relocated from extras.plugins to netbox.plugins

Changing in template_content.py the import for PluginTemplateExtension to the correct one seems to have fixed the issue.

$ ./manage.py migrate validity
Operations to perform:
  Apply all migrations: validity
Running migrations:
  Applying validity.0010_squashed_initial... OK

I'm running on netbox-validity 2.3.0, which is the latest on pip at time of writing.

amyasnikov commented 5 months ago

Hey @lc787, thanks for the detailed description of the issue, I've released version 2.3.1 with the fix.

P.S. It is interesting that the issue was python version specific. At least there was no error on Python 3.10.