Home > front end >  Django ModuleNotFoundError: No module named "\n 'hello"
Django ModuleNotFoundError: No module named "\n 'hello"

Time:12-06

I am following Django dj4e course. While going through setup that is linked here https://www.dj4e.com/assn/dj4e_ads1.md?PHPSESSID=991c1f9d88a073cca89c1eeda44f61d2 I got this weird error:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/cisco18/.virtualenvs/django3/lib/python3.6/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/cisco18/.virtualenvs/django3/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/home/cisco18/.virtualenvs/django3/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/cisco18/.virtualenvs/django3/lib/python3.6/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/home/cisco18/.virtualenvs/django3/lib/python3.6/site-packages/django/apps/config.py", line 212, in create
    mod = import_module(mod_path)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
... bunch of similar bootstrap lines ...
ModuleNotFoundError: No module named "\n    'hello"

I got this error when I did everything in the tutorial's setup and then copied the settings because they were missing something. I went through all those steps again and this weird error keeps popping up during python3 manage.py check. Here are my settings:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Used for a default title
APP_NAME = 'ChucksList'   # Add

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'g$iqqu&*mw4_sg3(#ld0sqaalxebel&168^yj%i&sgrw(fmn@w'

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

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',

    # Extensions - installed with pip3 / requirements.txt
    'django_extensions',
    'crispy_forms',
    'rest_framework',
    'social_django',
    'taggit',
    'home.apps.HomeConfig',

    # Sample Applications - don't copy
    """
    'hello.apps.HelloConfig',
    'users.apps.UsersConfig',
    'bookone.apps.BookoneConfig',
    'bookmany.apps.BookmanyConfig',
    'tracks.apps.TracksConfig',
    'views.apps.ViewsConfig',
    'tmpl.apps.TmplConfig',
    'gview.apps.GviewConfig',
    'route.apps.RouteConfig',
    'session.apps.SessionConfig',
    'authz.apps.AuthzConfig',
    'getpost.apps.GetpostConfig',
    'form.apps.FormConfig',
    'many.apps.ManyConfig',
    'myarts.apps.MyartsConfig',
    'menu.apps.MenuConfig',
    'forums.apps.ForumsConfig',
    'crispy.apps.CrispyConfig',
    'pics.apps.PicsConfig',
    'rest.apps.RestConfig',
    'favs.apps.FavsConfig',
    'favsql.apps.FavsqlConfig',
    'autos.apps.AutosConfig',
    'cats.apps.CatsConfig',
    'usermodel.apps.UsermodelConfig',
    'chat.apps.ChatConfig',
    'util.apps.UtilConfig',
    'well.apps.WellConfig',
    'tagme.apps.TagmeConfig',
    """
]

# When we get to crispy forms :)
CRISPY_TEMPLATE_PACK = 'bootstrap3'  # Add

# When we get to tagging
TAGGIT_CASE_INSENSITIVE = True

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',
    'social_django.middleware.SocialAuthExceptionMiddleware',   # Add
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
                'home.context_processors.settings',      # Add
                'social_django.context_processors.backends',  # Add
                'social_django.context_processors.login_redirect', # Add
            ],
        },
    },
]


WSGI_APPLICATION = 'mysite.wsgi.application'


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

"""
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/home/cisco18/mysite/db.sqlite3',
    }
}
"""

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'cisco18$default',
        'USER': 'cisco18',
        'PASSWORD': 'phone_8675309',
        'HOST': 'cisco18.mysql.pythonanywhere-services.com',
         'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.0/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.0/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.0/howto/static-files/

STATIC_URL = '/static/'

# Add the settings below

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    )
}

# Configure the social login
try:
    from . import github_settings
    SOCIAL_AUTH_GITHUB_KEY = github_settings.SOCIAL_AUTH_GITHUB_KEY
    SOCIAL_AUTH_GITHUB_SECRET = github_settings.SOCIAL_AUTH_GITHUB_SECRET
except:
    print('When you want to use social login, please see dj4e-samples/github_settings-dist.py')

# https://python-social-auth.readthedocs.io/en/latest/configuration/django.html#authentication-backends
# https://simpleisbetterthancomplex.com/tutorial/2016/10/24/how-to-add-social-login-to-django.html
AUTHENTICATION_BACKENDS = (
    'social_core.backends.github.GithubOAuth2',
    # 'social_core.backends.twitter.TwitterOAuth',
    # 'social_core.backends.facebook.FacebookOAuth2',

    'django.contrib.auth.backends.ModelBackend',
)

LOGOUT_REDIRECT_URL = '/'
LOGIN_REDIRECT_URL = '/'

# Don't set default LOGIN_URL - let django.contrib.auth set it when it is loaded
# LOGIN_URL = '/accounts/login'

# Needed for 3.2 and later
# https://stackoverflow.com/questions/67783120/warning-auto-created-primary-key-used-when-not-defining-a-primary-key-type-by
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

To be honest the error pop up doesn't tell me anything and I feel hopeless to solve that mystery however I really want to find the problem instead of starting over

CodePudding user response:

The problem was the """ in my INSTALLED_APPS. It must have been read as as a INSTALLED_APS string variable which caused issues. I deleted

"""
'hello.apps.HelloConfig',
other configs
"""

and the check went through. Thank you all for help I learned a lot out of your feedback

CodePudding user response:

I think something went wrong and executed the line import \n 'hello' by accident.

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'g$iqqu&*mw4_sg3(#ld0sqaalxebel&168^yj%i&sgrw(fmn@w'

Also, your key is secret for a reason.

  • Related