PyCharm django.core.exceptions.ImproperlyConfigured: settings are not configured

用PyCharm运行django项目脚本报错

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

可以看出报错原因是因为没有加载到django的setttings文件,那么我们怎么在不修改脚本的情况下在django环境下运行脚本呢?
其实PyCharm只需设置一个选项就可以完美解决。
PyCharm django.core.exceptions.ImproperlyConfigured: settings are not configured_第1张图片

F:\py-workspace\web\venv\Scripts\python.exe "D:\PyCharm 2018.2.4\helpers\pydev\pydevconsole.py" 65143 65144
import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend(['F:\\py-workspace\\web', 'D:\\PyCharm 2018.2.4\\helpers\\pycharm', 'D:\\PyCharm 2018.2.4\\helpers\\pydev'])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run("F:/py-workspace/web")
PyDev console: starting.
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Django 2.1.2

如上 运行之后PyCharm会默认在脚本启动之前运行jdango环境相关的代码。

你可能感兴趣的:(踩过的坑)