Greetings,
I have a problem with importing python file and calling it in views.py. In my case, I’m storing file (get.py) in the same directory as views.
Content of api.py:
import requests
from models import Company
from dateutil import parser
class GetRequest:
# some code...
I’m importing it as such in views.py:
from django.shortcuts import render, redirect
from django.views import generic
from django.views.generic.edit import FormMixin
from .models import Bill, Company, Product
from .forms import BillDetailsForm, ProductDetailsForm, CompanyDetailsForm
from datetime import datetime
from api import GetRequest # this one
class IndexView(generic.ListView):
template_name = 'racuni/index.html'
context_object_name = 'bills_list'
GetRequest()
I get the following error when I try to run server:
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "/home/person/.local/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/person/.local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
self.check(display_num_errors=True)
File "/home/person/.local/lib/python3.10/site-packages/django/core/management/base.py", line 487, in check
all_issues = checks.run_checks(
File "/home/person/.local/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/person/.local/lib/python3.10/site-packages/django/core/checks/urls.py", line 42, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/home/person/.local/lib/python3.10/site-packages/django/core/checks/urls.py", line 61, in _load_all_namespaces
url_patterns = getattr(resolver, "url_patterns", [])
File "/home/person/.local/lib/python3.10/site-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/person/.local/lib/python3.10/site-packages/django/urls/resolvers.py", line 696, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/person/.local/lib/python3.10/site-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/person/.local/lib/python3.10/site-packages/django/urls/resolvers.py", line 689, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.10/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 "/home/person/job/project/project/urls.py", line 21, in <module>
path('app/', include('app.urls'))
File "/home/person/.local/lib/python3.10/site-packages/django/urls/conf.py", line 38, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.10/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 "/home/person/job/project/app/urls.py", line 3, in <module>
from . import views
File "/home/person/job/project/app/views.py", line 7, in <module>
from api import GetRequest
ModuleNotFoundError: No module named 'api'
I have already try to import like .api or app.api but didn’t work. When i delete import of api, application starts working again.
Thank you for response