Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

ModuleNotFoundError: No module named 'models' #29

Open
The-Judge opened this issue Jun 27, 2019 · 1 comment
Open

ModuleNotFoundError: No module named 'models' #29

The-Judge opened this issue Jun 27, 2019 · 1 comment

Comments

@The-Judge
Copy link

I'm using:

  • Django 2.2.2
  • Python 3.7.3
  • django-background-task 0.1.8

To circumvent #28 , I applied this patch to models.py:

--- models.py	2019-06-27 13:20:34.467204000 +0000
+++ models.py_	2019-06-27 13:20:21.818710000 +0000
@@ -5,7 +5,11 @@
 from datetime import timedelta
 from hashlib import sha1
 import traceback
-from StringIO import StringIO
+import sys
+if sys.version_info[0] < 3:
+    from StringIO import StringIO
+else:
+    from io import StringIO
 import logging

 try:

When starting Django, startup now fails with ModuleNotFoundError: No module named 'models' instead.
The reason seems to be that in Python 3 the import behaviour for local modules has changed (see PEP 0328).

This patch for admin.py fixes the issue while keeping support for Python2 >=2.5:

--- admin.py	2019-06-27 13:22:49.715516000 +0000
+++ admin.py_	2019-06-27 13:29:52.350516000 +0000
@@ -1,6 +1,7 @@
+from __future__ import absolute_import
 from django.contrib import admin

-from models import Task
+from .models import Task

 class TaskAdmin(admin.ModelAdmin):
     display_filter = ['task_name']

The full error printed is this:

    Exception in thread django-main-thread:
    Traceback (most recent call last):
      File "/usr/local/lib/python3.7/threading.py", line 917, in _bootstrap_inner
        self.run()
      File "/usr/local/lib/python3.7/threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)
      File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
        fn(*args, **kwargs)
      File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
        autoreload.raise_last_exception()
      File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
        raise _exception[1]
      File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 337, in execute
        autoreload.check_errors(django.setup)()
      File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
        fn(*args, **kwargs)
      File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 122, in populate
        app_config.ready()
      File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/apps.py", line 24, in ready
        self.module.autodiscover()
      File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
        autodiscover_modules('admin', register_to=site)
      File "/usr/local/lib/python3.7/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
        import_module('%s.%s' % (app_config.name, module_to_search))
      File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
      File "<frozen importlib._bootstrap>", line 983, in _find_and_load
      File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 728, in exec_module
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "/usr/local/lib/python3.7/site-packages/background_task/admin.py", line 3, in <module>
        from models import Task
    ModuleNotFoundError: No module named 'models'
@Ashjoker24
Copy link

I am also facing the same issue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants