0

I splited my settings.py like this:

# __init__.py
from .base import *  # noqa

try:
    from .local_settings import *  # noqa
except ImportError:
    message = 'ERROR: Unable to import local_settings. Make sure it exists.'
    raise ImportError(message)

# base.py
"""
Django settings for app project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/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.dirname(os.path.abspath(__file__))))

# Add the apps folder to python path
os.sys.path.append(os.path.join(BASE_DIR, 'apps'))


# Application definition

INSTALLED_APPS = [
    '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 = 'app.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',
            ],
        },
    },
]

WSGI_APPLICATION = 'app.wsgi.application'


# 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',
    },
]

and:

# local_settings.py
import os
from .base import BASE_DIR

# 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 = '670b9a538dfe8545f4eff723345da43211084a05f520a2d638'

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

ALLOWED_HOSTS = []


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'run/db.sqlite3'),
    }
}


# 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/'

Every command works good except python manage.py startapp myapp [or any other app name] ./apps/myapp. Every time I run it command I get this error:

CommandError: 'myapp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.

I found it related with my code in __init__.py file. I mean if I comment all the codes, that error will be disappear. Why does it happend? How can I solve it?

With Directory

Without Directory

1 Answers1

0

As @juggernaut has stated. The error is happening when you use the character *, which imports everything and hence overwrites some internal.

When you run the command python manage.py startapp myapp xxxx The myapp section conflicts with an existing module and hence causes that error.

Try importing it manually instead of using from .base import * # noqa. This way you only import the modules that you will need and would not lead to any errors.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • Hi, but this command work well: `python manage.py startapp myapp # without any direction` – محمد علی امینی Mar 10 '20 at 17:09
  • Yes, this command causes an error: `python manage.py startapp myapp ./apps/myapp`, but this one work well: `python manage.py startapp myapp` – محمد علی امینی Mar 10 '20 at 17:14
  • Those commands are 2 different commands. The first is referring to the project dir (the one which django-admin.py creates). The second is where it executes your existing django app. You would typically only use the latter method unless you want to create a new django app (see [here](https://stackoverflow.com/questions/33243661/startapp-with-manage-py-to-create-app-in-another-directory) for more) – AzyCrw4282 Mar 10 '20 at 17:23
  • I has added 2 images to my question. Please see those. – محمد علی امینی Mar 10 '20 at 17:49
  • Try [this](https://stackoverflow.com/questions/7019070/cannot-start-any-django-app) please. Any-one of it could be the solution – AzyCrw4282 Mar 10 '20 at 18:01
  • Not exactly, I now create a new app tricky. I go into the apps folder then use `python ../manage.py startapp myapp`. But I found out that reason. It happened because I have to create app's folder before starting it; If I don't create it, django get me another error. I thing it's related with django implementation (or django bug). – محمد علی امینی Mar 11 '20 at 06:06