python shell 환경셋팅

BackEnd_Ash.log·2020년 3월 24일
0

설치

바로 python manage.py shell 해더 쉘 환경에 들어가도 되지만
좀더 편하게 관리 하기 위해서 ipython 을 설치하자
conda install ipython 을 해도 되지만 ,
jupyter 모드가 ipython 을 사용해서
conda install jupyter 를 해주자

conda install jupyter

 jakdu  ~/django_project/askcompany   feature/django_study_two ✚ ● ⍟1  ipython                                                          ✔
Python 3.7.6 (default, Jan  8 2020, 13:42:34)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

위처럼 뜬다면 설치가 정상적으로 된것이다 .

settings.py

INSTALLED_APPS = [
	....
    'django_extensions',
   ...
]

이렇게 추가를 해준다.

그리고 다시 python manage.py shell 로 들어가면

jakdu  ~/django_project/askcompany   feature/django_study_two ✚ ● ⍟1  python manage.py shell                                           ✔
Python 3.7.6 (default, Jan  8 2020, 13:42:34)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

로 나오게 된다.

shell 환경 셋팅

manage.py 를 열어보면
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'askcompany.settings')

라고 적혀있는부분이 있을것이다 .
장고가 실행될때 필요한 환경변수이다 .

바로 장고 settings 의 위치를 알려달라는것이다 .

python manage.py shell 환경으로 가서

In [5]: os.environ['DJANGO_SETTINGS_MODULE']='askcompany.settings'

In [6]: import django

In [7]: django.setup()

를 입력해준다.

In [8]: from instagram.models import Post

In [9]: Post.objects.all()
Out[9]: <QuerySet [<Post: Custom Post object(asdf)>, <Post: Custom Post object(sadfzxcv)>, <Post: Custom Post object(3asdfzxcv)>]>

만약 러닝머신을 한다거나 , 크롤링을 할때 쥬피터를 사용하고싶을때 , 쥬피터를 많이 사용합니다 .

shell_plus

jakdu  ~/django_project/askcompany   master ● ⍟3  python manage.py shell_plus --print-sql --ipython                                  1# Shell Plus Model Imports
from instagram.models import Post
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
# Shell Plus Django Imports
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Avg, Case, Count, F, Max, Min, Prefetch, Q, Sum, When, Exists, OuterRef, Subquery
from django.utils import timezone
from django.urls import reverse
Python 3.7.6 (default, Jan  8 2020, 13:42:34)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

shell_plus 특징은
몇몇 기능들을 자동으로 import 해준다.
편리하지만 , 단점은
Post 가 만약에 다른곳에 겹치면 오버라이트 된다.

ex )

In [9]: Post.objects.all()[1:3:2]
SELECT "instagram_post"."id",
       "instagram_post"."message",
       "instagram_post"."photo",
       "instagram_post"."is_public",
       "instagram_post"."created_at",
       "instagram_post"."updated_at"
  FROM "instagram_post"
 ORDER BY "instagram_post"."id" DESC
 LIMIT 2
OFFSET 1

Execution time: 0.000323s [Database: default]
Out[9]: [<Post: Custom Post object(두번째)>]

오류

만약에 되지 않고
에러가 나온다면 예를들어서
Unknown command: 'shell_plus'
이러한 에러가 발생한다면 ,

pip install django-extensions

해주고 나서 settings.py 로가서

INSTALLED_APPS = [
	....
    'django_extensions',

추가를 해준다.

profile
꾸준함이란 ... ?

0개의 댓글