简体   繁体   中英

ImportError: Couldn't import Django. PYTHONPATH

There was an error account.User that has not been installed but I solved this problem. After that, another error says The SECRET_KEY setting must not be empty.

I don't know whether my method to solve this problem is correct or not, I applied some solutions with Google. But now, there is an error 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? 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? But I already installed django and virtualenv. I don't know how to do it.

Recent error:

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?

MY code manage.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"

Your traceback is telling you exactly what is wrong:

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?

So you should relaunch your IDE or activate your environnement, as Django cannot be launch

I am using a Mac OSX 10.13.6. I have Python 3.9 installed.

I activated my venv, then installed Django. When I use 'pip list' or 'pip3 list', it lists Django as one of the Python packages that are installed. In addition, I was able to create a project using "django-admin startproject. If Django was not installed or I was not using venv, then I could not execute the django-admin command.

Also, I checked the venv/lib/python3.9/site-packages and django and other Python packages are installed in that directory. AND I did ' export PYTHONPATH=/Library/Frameworks/Python.framework/Versions/3.9 '.

So, it is somewhat confusing why python3 is not finding Django. Any suggestions?

I installed django through this command

pip install django

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM