首页 > 解决方案 > 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"

标签: pythonpython-3.xdjango

解决方案


您的回溯准确地告诉您出了什么问题:

无法导入 Django。您确定它已安装并在您的 PYTHONPATH 环境变量中可用吗?您是否忘记激活虚拟环境?

因此,您应该重新启动 IDE 或激活您的环境,因为 Django 无法启动


推荐阅读