pycharm无法加载Django应用程序

时间:2018-11-22 04:50:26

标签: python django pycharm

PyCharm 中,我在virtual environment中配置了 Django ,我所做的是:

  1. 以前,我在Windows 10上安装了python 3.6,因此我将其卸载了,并且
  2. 已安装Python 3.7
  3. pip install virtualenv
  4. vertualenv venv,然后将其激活
  5. pip install django
  6. 还通过djangorestframework安装了coreapirequestspip
  7. django-admin startproject hasslefreeaccounts
  8. 在PyCharm中打开项目,然后:文件>设置>语言和 框架,并将我的项目配置为:
  9. 输入项目根目录settings.py和manage.py路径(请参见下图)

enter image description here

,然后创建了两个名为 customer master 的应用程序
然后将运行配置配置为:

enter image description here

此后(我正在使用pycharm professional)一切正常,除了导入应用程序时PyCharm当我以其他方式导入django应用程序时给我错误:

from masters.models import some_class

pycharm说找不到模块,以为当我运行项目时它运行正常,没有错误。
所以我尝试做:

from ..master.models import some_class

这使得PyCharm导入应用程序(请参见下图)

enter image description here

pycharm中的此内容未显示任何错误,但在运行上显示的错误为:

ValueError: attempted relative import beyond top-level package

其他详细信息:

这是项目解释器设置

enter image description here

Settings.py为:

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__)))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'KEY_HERE'

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

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'customers',
    'masters',
]

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 = 'hasslefreeaccounts.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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',
            ],
        },
    },
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    )

}

WSGI_APPLICATION = 'hasslefreeaccounts.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/2.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/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'  # 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'

已更新:

基本上PyCharm正在读取我的应用,位于上方的一个目录中
我也尝试将项目源根目录也更新到上面的一个目录,但这在项目解释器设置

中没有帮助

1 个答案:

答案 0 :(得分:0)

不确定,但这对我有所帮助!

enter image description here

如图所示,virtualenv应该在项目目录中,而不是外部,这对我有帮助!