[VSCode] Python Interactive window 사용하여 코딩하기

2022. 10. 6. 23:08꿀팁 분석 환경 설정/파이썬 개발 팁

728x90

가끔 jupyter를 사용하다 보면 버벅거릴 떄가 있고, 메모리를 많이 차지하는 경우가 있습니다.

이게 실제로 효율적인지는 모르겠지만, 우연히 발견하게 된 interactive view가 도움이 될 수도 있을 것 같아 공유드립니다. 

물론 vscode 주피터에서 작업을 하게 되면, 기존에 관습대로 할 수 있어서 좋지만, 실제 학습 코드 개발이나 테스트하는 입장에서는 py에서 작업을 하는 것이 더 좋을 것 같다고 생각이 듭니다.

 

일단 아마도 jupyter extension이 설치가 되어야 이러한 작업이 가능한 것 같습니다.

설치가 안된 상태에서는 작동하지 않았습니다.

인텔리센스

Python Interactive 창에는 코드 완성, 멤버 목록, 메서드에 대한 빠른 정보 및 매개 변수 힌트와 같은 전체 IntelliSense가 있습니다. 코드 편집기에서와 마찬가지로 Python Interactive 창에서 생산적으로 입력할 수 있습니다.

 

사용 범은 참고 자료에 있는 링크를 보면 자세히 나오는데 

간단하게 예시를 적으면 다음과 같습니다.

 

항상 키게 하는 법(옵션)

settings.json에 추가하기

"jupyter.sendSelectionToInteractiveWindow": true

추가하고 shift + enter를 누르면 항상 interactive view가 생성됨.

 

Interactive View 생성

 

F1 -> Python: Create Python Interactive Window

 

예제 코드

코드 부분은 # %%

마크다운 부분은 #%%[markdown]

 

이런 식으로 작성해주면 됩니다.

# %%
print("hi")
# %%
print("이게 되네??")
print("빠른 지는 모르겠네?")
# %%
import sys
import numpy as np

data = np.arange(10)
# %%
print("?")
# %%
import matplotlib.pyplot as plt

plt.hist(np.random.rand(10))
plt.show()
# %%
data


#%%[markdown]
# # 하이
# ## 머해

# %%

 

주피터로 내보내기

결과물을 우리가 아는 형식으로 내보낼 때 .ipynb로 내보낼라면 다음과 같이 해야합니다.

1. Ctrl + Shift + P
2. Jupyter: Export Current File and Output as Jupyter Notebook 검색

3. 변경

 

 

 

짜잘한 팁이지만 도움이 되시기를 바랍니다!

즐거운 코딩 되시길!

 

 

참고

https://code.visualstudio.com/docs/python/jupyter-support-py

 

Working with Jupyter code cells in the Python Interactive window

Working with Jupyter code cells in the Python Interactive window

code.visualstudio.com

https://stackoverflow.com/questions/64730660/how-do-i-find-excute-python-interactive-mode-in-visual-studio-code

 

How do I find/excute Python Interactive Mode in Visual Studio Code?

I'm unable to find/execute the interactive mode in visual studio code - could you guys step-by-step me? Seems like a ridiculous question, I guess, but none of the waypoints I Googled seem to work; ...

stackoverflow.com

 

728x90