My Django project have a problem when it starts
2022-09-01 09:22:51,525 [MainThread:25612] [django.utils.autoreload:612] [INFO]- Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Python37\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Python37\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Python37\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Python37\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Python37\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Python37\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Python37\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python37\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python37\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python37\lib\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 "D:\code\ido-jira-plug\IdoJiraPlug\IdoJiraPlug\urls.py", line 23, in <module>
path('jira/', include('jira_plug.urls', namespace='jira_plug')),
File "C:\Python37\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python37\lib\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 "D:\code\ido-jira-plug\IdoJiraPlug\jira_plug\urls.py", line 5, in <module>
from . import api
File "D:\code\ido-jira-plug\IdoJiraPlug\jira_plug\api\__init__.py", line 3, in <module>
from .config import *
File "D:\code\ido-jira-plug\IdoJiraPlug\jira_plug\api\config.py", line 5, in <module>
from jira_plug.utils import JiraAction
File "D:\code\ido-jira-plug\IdoJiraPlug\jira_plug\utils\__init__.py", line 6, in <module>
from .rule import *
File "D:\code\ido-jira-plug\IdoJiraPlug\jira_plug\utils\rule.py", line 4, in <module>
from jira_plug.serializers import RuleConfigInfoSerializer
File "D:\code\ido-jira-plug\IdoJiraPlug\jira_plug\serializers\__init__.py", line 4, in <module>
from .request_serializer import *
File "D:\code\ido-jira-plug\IdoJiraPlug\jira_plug\serializers\request_serializer.py", line 6, in <module>
from jira_plug.utils import JiraAction, get_msg_data, get_rule_detail, handle_message_content
ImportError: cannot import name 'get_rule_detail' from 'jira_plug.utils' (D:\code\ido-jira-plug\IdoJiraPlug\jira_plug\utils\__init__.py)
This is my project,
IdoJiraPlug
- jira_plug
- __init__.py
- api
__init__.py
config.py
- serializers
__init__.py
request_serializer.py
- utils
__init__.py
rule.py
- urls.py
- manage.py
jira_plug.utils.__init__.py
from .rule import *
jira_plug.utils.rule.py
...
from jira_plug.serializers import RuleConfigInfoSerializer
...
jira_plug.serializers.__init__.py
from .request_serializer import *
jira_plug.serializers.request_serializer.py
...
from jira_plug.utils import JiraAction, get_msg_data, get_rule_detail, handle_message_content
...
I feel like this is because rule.py and request_serializer.py import each other,but I don't known real reason. I try to use 'from jira_plug.utils.rule import get_rule_detail, handle_message_content', but it still has this error.
CodePudding user response:
in your jira_plug
app in utils.py
file you are don't have get_rule_detail
class or function or name of variable.
If you are sure, that it is exists - try to check every letter in name in utils.py, probably you find the error.
CodePudding user response:
I solve this problem.The real reason is circular import.When django starts,urls.py will be used by import_module().python will begin to load module step by step according to jira_plug.urls.py.
- jira_plug.urls.py, 5 line:
from . import api
,then python will to load jira_plug.api.__init__.py module. - jira_plug.api.__init__.py, 3 line:
from .config import *
,then python will to load jira_plug.api.config.py module. - jira_plug.api.config.py, 5 line:
from jira_plug.utils import JiraAction
,then python will to load jira_plug.utils.__init__.py module. - jira_plug.utils.__init__.py, 6 line:
from .rule import *
,then python will to load jira_plug.utils.rule.py module. - jira_plug.utils.rule.py, 4 line:
from jira_plug.serializers import RuleConfigInfoSerializer
,then python will to load jira_plug.serializers.__init__.py module. - jira_plug.serializers.__init__.py, 4 line:
from .request_serializer import *
,then python will to load jira_plug.serializers.request_serializer.py module. - jira_plug.serializers.request_serializer.py, 6 line:
from jira_plug.utils import JiraAction, get_msg_data, get_rule_detail, handle_message_content
, then python will to load jira_plug.utils package.
Step 5 , Step 6 and Step 7 lead to this error. A --> B --> A, module A have not be loaded, B module also load A module,so python interpreter caused "ImportError".
The best solution is planning to abstract common package.
Temporary solution is modify jira_plug.api.config.py.
# from jira_plug.utils import JiraAction
from jira_plug.utils.call_jira import JiraAction
This will lead to disappear after step 3. But this is a hidden danger, I already have to abstract common methods. This solution is just an example!