报错如下:
python
python manage.py shell
Traceback (most recent call last):
File "/Users/z/Documents/project/c/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/z/.virtualenvs/c/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/Users/z/.virtualenvs/c/lib/python3.12/site-packages/django/core/management/__init__.py", line 416, in execute
django.setup()
File "/Users/z/.virtualenvs/c/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/z/.virtualenvs/c/lib/python3.12/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/z/.virtualenvs/c/lib/python3.12/site-packages/django/apps/config.py", line 193, in create
import_module(entry)
File "/opt/homebrew/Cellar/[email protected]/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'xxx'
原因 是 manage.py文件里的内容没有导入项目的路径到环境变量里,正确的内容如下:
python
import os
import sys
if __name__ == "__main__":
app_path = os.path.dirname(sys.path[0])
sys.path.append(app_path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)