Programming-[Backend]/Django

[TIL] Pycharm에서 Python Console에 shell_plus 연결하기

컴퓨터 탐험가 찰리 2022. 7. 5. 20:06
728x90
반응형

shell_plus 설치

pip install django-extensions
settings.py - INSTALLED_APPS에 ‘django_extensions’ 추가 (언더바임을 주의)

 

실행

 

그냥 터미널에서 아래 명령어로 실행해도 된다.

python manage.py shell_plus

 

 

Django Console 이용하기

 

 

터미널창 아래에 Python Console 을 이용하면 Pycharm의 설정에 연결된 python interpreter로 바로 연결되어 테스트가 가능하다.

Preferences(설정) > Build, Execution, Deployment > Console > Django Console에 들어가서 Starting script 부분에 아래 복사본을 넣는다.

 

아래 코드는 shell plus로 Django console을 실행해주는 script 이다.

import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run(PROJECT_ROOT)

from django_extensions.management import shells
from django.core.management.color import color_style
imported_items = shells.import_objects({}, color_style())
for k, v in imported_items.items():
    globals()[k] = v

 

 

기존 켜져있던 Python Console 을 끄고, 새로 켜면(Django Console) shell plus로 실행되면서 필요한 import 등을 다 해준다.

이제 User. 만 쳐도 objects 등 추천 단어가 자동으로 검색되는 것을 확인할 수 있다.

 

 

 

 

 

---

 

에러나는 경우

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

 

 

에러 메시지에서 보는 것처럼 File > Settings 메뉴를 클릭해 설정 팝업이 뜨면 Build, Execution, Deployment > Console > Django Console 메뉴에서 DJANGO_SETTINGS_MODULE 환경변수를 아래와 같이 알맞은 값으로 재설정한다.

 

https://wikidocs.net/6625

 


 

참조

 

1. Stack over flow 글

https://stackoverflow.com/questions/19868449/run-shell-plus-through-pycharm

728x90
반응형