python - ImportError:无法导入 Django。蟒蛇路径
问题描述
有一个错误account.User
尚未安装,但我解决了这个问题。之后,另一个错误说The SECRET_KEY setting must not be empty.
我不知道我解决这个问题的方法是否正确,我用谷歌应用了一些解决方案。但是现在,有一个错误ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
但是我已经安装了django和virtualenv。我不知道该怎么做。
最近的错误:
Traceback (most recent call last):
File "/Users/leejunseo/PycharmProjects/ITM coding/manage.py", line 10, in main
from django.core.management import execute_from_command_line
File "/Users/leejunseo/PycharmProjects/ITM coding/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 12, in <module>
from django.conf import settings
File "/Users/leejunseo/PycharmProjects/ITM coding/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 21, in <module>
from .base import *
ModuleNotFoundError: No module named 'django.conf.base'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/leejunseo/PycharmProjects/ITM coding/manage.py", line 21, in <module>
main()
File "/Users/leejunseo/PycharmProjects/ITM coding/manage.py", line 12, in main
raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
我的代码管理.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tothegemList.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
__init__.py
import functools
import os
import pkgutil
import sys
from argparse import _SubParsersAction
from collections import defaultdict
from difflib import get_close_matches
from importlib import import_module
import django
from django.apps import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import (
BaseCommand, CommandError, CommandParser, handle_default_options,
)
from django.core.management.color import color_style
from django.utils import autoreload
def find_commands(management_dir):
"""
Given a path to a management directory, return a list of all the command
names that are available.
"""
command_dir = os.path.join(management_dir, 'commands')
return [name for _, name, is_pkg in pkgutil.iter_modules([command_dir])
if not is_pkg and not name.startswith('_')]
. . .
conf/__init__py
import warnings
from pathlib import Path
import django
from django.conf import global_settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.deprecation import RemovedInDjango40Warning
from django.utils.functional import LazyObject, empty
from .base import *
env_name = os.getenv('ENV_NAME', 'local')
if env_name == 'prod':
from .prod import *
elif env_name == 'stage':
from .stage import *
else:
from .local import *
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
解决方案
您的回溯准确地告诉您出了什么问题:
无法导入 Django。您确定它已安装并在您的 PYTHONPATH 环境变量中可用吗?您是否忘记激活虚拟环境?
因此,您应该重新启动 IDE 或激活您的环境,因为 Django 无法启动
推荐阅读
- android - 主要活动空闲时Android线程变慢
- c# - 如何在 ASP.NET MVC 中根据条件打开/关闭基于身份的身份验证
- sql - SQL - 添加列
- php - 简单的 if 语句返回意外值
- php - 有什么方法可以绕过 Defined("App") 或退出("no access")
- javascript - 单击按钮后,将事件侦听器添加到窗口
- python - 无法从类中正确获取列表数据
- tcl - 无法使用 exit 命令终止 TCL 脚本,因为它未被识别
- javascript - 如何使用 GitHub Actions 在我的项目中的特定目录中运行 ESLint
- here-api - 反向地理编码端点响应:“访问”字段