'Can't import a module in Django project
This is my django project folder:
mysite
├── mypage
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── admin.cpython-38.pyc
│ │ ├── apps.cpython-38.pyc
│ │ ├── models.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── views.cpython-38.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ └── __init__.cpython-38.pyc
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── mysite
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── utils
├── __init__.py
├── __pycache__
│ └── __init__.cpython-38.pyc
└── util.py
This is the error message that I get when I run the server.
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
self.check(display_num_errors=True)
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/core/management/base.py", line 487, in check
all_issues = checks.run_checks(
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/core/checks/urls.py", line 24, in check_resolver
return check_method()
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 480, in check
for pattern in self.url_patterns:
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 696, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 689, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/gigidagostino/Desktop/django_experment/mysite/mysite/urls.py", line 21, in <module>
path('mypage/', include('mypage.urls'))
File "/Users/gigidagostino/Desktop/django_experment/venv/lib/python3.8/site-packages/django/urls/conf.py", line 38, in include
urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/gigidagostino/Desktop/django_experment/mysite/mypage/urls.py", line 3, in <module>
from . import views
File "/Users/gigidagostino/Desktop/django_experment/mysite/mypage/views.py", line 2, in <module>
from mysite.utils.util import *
ModuleNotFoundError: No module named 'mysite.utils'
This is the view.py in mypage app folder:
from django.http import HttpResponse
from mysite.utils.util import *
def index(request):
print_hi()
return HttpResponse("Hello")
So the question is: How do I import util.py in view.py? It does work if I just move the utils inside mypage folder, but I'd like to know if it's possible to do it without moving utils folder.
Solution 1:[1]
You can use relative imports
like
from .utils.util import *
from ..utils.util import *
from ...utils.util import *
depending on your project structure
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PleSo |
