Django No Module Named... when calling a function in another folder

I have a Django application and in my views.py file I am trying to call a function that is located in another file in another folder, still in the same app.

My folder structure looks like:

.administraion
├── background_tasks
│   ├── magic_update_set.py
├── migrations
│   ├── __init__.py
├── __init__.py
├── admin.py
├── config.py
├── models.py
├── tests.pu
├── urls.py
└── views.py

In the magic_update_set.py file I have the following function:

def magic_set_update_bg_task(options):
    ...

In my vioews.py I have:

from background_tasks.magic_update_set import magic_set_update_bg_task

...

def update_magic_set_function_queue(request):
    options = {
        'code': request.POST['code'],
    }
    magic_set_update_bg_task(options, verbose_name='Set Update')
    return HttpResponse(status=200)

Error:

Traceback (most recent call last):
  File "C:\Users\rossw\Documents\Projects\card_companion\manage.py", line 22, in <module>
    main()                                                                               
  File "C:\Users\rossw\Documents\Projects\card_companion\manage.py", line 19, in main    
    execute_from_command_line(sys.argv)                                                  
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\core\management\base.py", line 393, in execute
    self.check()
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\core\management\base.py", line 419, in check
    all_issues = checks.run_checks(
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\urls\resolvers.py", line 416, in check
    for pattern in self.url_patterns:
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\urls\resolvers.py", line 602, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\urls\resolvers.py", line 595, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\rossw\Documents\Projects\card_companion\core\urls.py", line 12, in <module>
    path('administration/',    include('apps.administration.urls')),
  File "C:\Users\rossw\Documents\Projects\card_companion\card_companion_venv\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\rossw\Documents\Projects\card_companion\apps\administration\urls.py", line 3, in <module>
    from apps.administration.views import administration_tasks, update_magic_set_function_queue
  File "C:\Users\rossw\Documents\Projects\card_companion\apps\administration\views.py", line 6, in <module>
    from background_tasks.magic_update_set import magic_set_update_bg_task
ModuleNotFoundError: No module named 'background_tasks'


You need to create an __init__.py inside your backround task folder

Back to Top