Using Python 3.9 and Django 3.1.3 server crashes when re-launched

1k views Asked by At

this is my first question at Stack overflow. I have been learning Python for about a month and Django for two weeks. I tried to run a second server on my MacBook Pro and then had these problems.(the error messages from my terminal below) The second server included some other applications including Pihole. I deleted the second server files and re-booted, problem solved! The next thing was that I tried editing my Views.py file in Django and then the same error message came back when I re-booted my Django server in the virtual environment folder. See errors below:

mymac@michaels-MacBook-Pro mfdw_root % python manage.py runserver
Traceback (most recent call last):
  File "/Users/mymac/Desktop/mfdw_project/mfdw_root/manage.py", line 22, in <module>
    main()
  File "/Users/mymac/Desktop/mfdw_project/mfdw_root/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 83, in __getattr__
    self._setup(name)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 177, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 786, in exec_module
  File "<frozen importlib._bootstrap_external>", line 923, in get_code
  File "<frozen importlib._bootstrap_external>", line 853, in source_to_code
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/mymac/Desktop/mfdw_project/mfdw_root/mfdw_site/settings.py", line 123
    'DIRS': [BASE_DIR / 'mfdw_site/templates'],
    ^
SyntaxError: illegal target for annotation


end of errors********

This is my first question at Stack Overflow and I am very uncertain right now if I will ever get my server running again on my MacBook Pro with Python 3.9.0 and Django 3.1.3 This is the error I get above, and I can't connect to 127.0.0.1 , the localhost on my MacBook Pro. Please try and give me a clue as to how to proceed. I have been following the lessons of djangobook.com .

Below is my settings.py file:

"""
Django settings for mfdw_site project.

Generated by 'django-admin startproject' using Django 3.1.3.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '# secret key goes here, it is present in actual code'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'pages.apps.PagesConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mfdw_site.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'mfdw_site/templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mfdw_site.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

'DIRS': [BASE_DIR / 'mfdw_site/templates'],

STATICFILES_DIRS = [BASE_DIR / 'mfdw_site/static')]

Also, is there a path problem with my Django installation? I am running on a MacBook Pro which is properly updated to use Python 3.9 . I think it might have something to do with this part of the errors:

"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()

Does this seem reasonable? The flagged code in settings.py occurs on line 123. Also it looks to me like python 3.9 has an included django installation which is confused via path with the mfdw_project files. Am I right and what should I do about it?

1

There are 1 answers

5
moshe742 On

Please read the error carefully, it says that your settings file is misconfigured on line 123 and gives you the line that is offending with the ^ as a sign on where it happens:

'DIRS': [BASE_DIR / 'mfdw_site/templates'],
^

and it says there is a syntax error, so you can tell that the problem is you wrote something not according to the syntax of python. According to this, you should check what is going on on the line before this line (line 122), check what happens if you put this line in comment (with #), it will either find another error on the next line, find an error somewhere later or run smoothly.

If it finds an error on the next line, you can check what you did wrong on the line before this one (line 122). If it finds an error somewhere later on you can understand the problem is with the actual line (line 122) and it's probably the / you have there, a list sepperator is comma (,), not /...

Edit: First, thanks for adding the settings file, it helps to find the error after we know what to look for. The offending line is 123 and it looks like this:

'DIRS': [BASE_DIR / 'mfdw_site/templates'],

This has 3 problems:

  1. The 'DIRS' shouldn't be a string, it should be a variable, so remove the '.
  2. The line shouldn't have a colon, this is not a dictionary or a function definition.
  3. I understand that you want to have the path as BASE_DIR/mfdw_site/templates, assuming so, it can be [f'{BASE_DIR}/mfdw_site/templates'] and a few other options, but this is the easiest