PyCharm django.core.exceptions.ImproperlyConfigured: settings are not configured
阿新 • • 發佈:2018-12-18
用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只需設定一個選項就可以完美解決。
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環境相關的程式碼。