จะใช้คอนโซล shell_plus (iPython) ใน PyCharm ได้อย่างไร

ฉันกำลังเรียนรู้ที่จะพัฒนาใน Django และฉันใช้ PyCharm กับโปรเจ็กต์ของฉัน และฉันต้องการใช้คอนโซล iPython ในนั้น

iPython เปิดตัวแล้ว นี่ไม่ใช่ปัญหา

ปัญหาคือเมื่อฉันเปิด iPython จากคอนโซล โมเดลของฉันและคลาสยูทิลิตี้อื่นๆ ทั้งหมดจะถูกนำเข้า

นี่คือสิ่งที่เกิดขึ้นเมื่อฉันเปิดคำสั่ง python3 manage.py shell_plus:

# Shell Plus Model Imports
from app.models.models import Model1, Model2, Model3
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
/myproject/env/lib/python3.7/site-packages/IPython/core/history.py:226: UserWarning: IPython History requires SQLite, your history will not be saved
  warn("IPython History requires SQLite, your history will not be saved")
Python 3.7.4 (default, Jul 23 2019, 18:02:54) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: 

ในขณะเดียวกันเมื่อฉันเริ่ม Python Console ใน PyCharm ไม่มีการนำเข้าใด ๆ และฉันต้องนำเข้าโมเดลทั้งหมดด้วยตนเอง นี่คือสิ่งที่เกิดขึ้นใน PyCharm Python Console:

/home/user/myproject/env/bin/python /snap/pycharm-community/188/plugins/python-ce/helpers/pydev/pydevconsole.py --mode=client --port=33543
/home/user/myproject/env/lib/python3.7/site-packages/IPython/core/history.py:226: UserWarning: IPython History requires SQLite, your history will not be saved
  warn("IPython History requires SQLite, your history will not be saved")
import sys, django
print('Python %s on %s' % (sys.version, sys.platform))
Python 3.7.4 (default, Jul 23 2019, 18:02:54) 
Type 'copyright', 'credits' or 'license' for more information
In[2]:

แน่นอนว่าคำสั่งถูกดำเนินการบนคำสั่ง shell_plus ซึ่งไม่ได้อยู่ในคอนโซล PyCharm

และนี่คือเวอร์ชันที่ใช้

Ubuntu = 18.04 LTS
python = 3.7.4
iPython = 7.13.0
Django = 3.0.5
PyCharm = 2019.3.4 Community

ขอบคุณสำหรับความช่วยเหลือของคุณ ฉันสามารถช่วยฉันได้


person Bravo2bad    schedule 08.04.2020    source แหล่งที่มา


คำตอบ (2)


คุณสามารถรันคำสั่ง django ที่กำหนดเองบน pycharm โดยใช้ Ctrl + ALT + R หรือตามเมนู Tools -> Run manage.py tasks

person Umar    schedule 08.04.2020
comment
ฉันไม่มีตัวเลือกนี้ใน PyCharm 2019.3.4 คุณรู้ไหมว่าฉันควรใช้สคริปต์ใดเพื่อเรียก shell_plus ขอบคุณสำหรับความช่วยเหลือของคุณต่อไป - person Bravo2bad; 09.04.2020
comment
คุณใช้เวอร์ชันชุมชนหรือเวอร์ชันมืออาชีพหรือไม่? เนื่องจากตัวเลือกนี้มีเฉพาะในเวอร์ชันมืออาชีพเท่านั้น ขออภัย ลืมบอกไว้ก่อน - person Umar; 09.04.2020
comment
เวอร์ชันชุมชน ใช่ขอโทษ. ฉันลืมไปว่ามันเป็น IDE สองเวอร์ชัน มีวิธีอื่นในการใช้เชลล์ของ iPython ใน Python Console หรือไม่ - person Bravo2bad; 09.04.2020
comment
ขอโทษที ฉันไม่รู้ - person Umar; 09.04.2020
comment
ขอบคุณที่พยายามต่อไป - person Bravo2bad; 11.04.2020

ฉันพบสิ่งนี้ทางออนไลน์และทำงานได้ดี (พร้อมการโหลดอัตโนมัติ)

???? หมายเหตุ: ใช้งานได้เฉพาะเมื่อคุณแกล้งทำเป็นใช้คุณสมบัติ Django Console ใน Pycharm

วางรหัสด้านล่างลงใน Build, Execution, Deployment > Console > Django Console > Starting Script

# requirements: `django_extensions`, `IPython`
import sys
import django

from IPython.core.getipython import get_ipython
from django_extensions.management.notebook_extension import load_ipython_extension

print('Python %s on %s' % (sys.version, sys.platform))
print('Django %s' % django.get_version())

sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
if 'setup' in dir(django):
    django.setup()
ipython = get_ipython()
load_ipython_extension(ipython)

# NOTE: If you don't use the autoreload feature, don't paste the three lines below
load_ipython_extension(ipython)
ipython.extension_manager.load_extension('autoreload')
ipython.magics_manager.registry['AutoreloadMagics'].autoreload('2') 
person EsseTi    schedule 29.12.2020