본문으로 건너뛰기
Advertisement

Ch 1.3 개발 환경 구성

파이썬 인터프리터를 설치했다면, 이제 코드를 효율적으로 작성할 수 있는 개발 환경을 구성합니다. 에디터 설정부터 코드 품질 도구까지 실무에서 바로 쓸 수 있는 환경을 만들어봅시다.

1. VS Code 설정

Visual Studio Code는 가볍고 확장성이 뛰어나 파이썬 개발에 가장 많이 사용되는 에디터입니다.

필수 확장 설치

  1. VS Code 다운로드
  2. 확장(Extensions) 탭에서 설치:
    • Python (Microsoft) — 파이썬 기본 지원
    • Pylance (Microsoft) — 고속 언어 서버, 타입 체크
    • Ruff — 빠른 린터/포매터
    • Python Debugger (Microsoft) — 디버깅 지원

파이썬 인터프리터 선택

Ctrl+Shift+P (macOS: Cmd+Shift+P)
→ "Python: Select Interpreter"
→ 원하는 파이썬 버전 또는 가상환경 선택

settings.json 설정 예시

.vscode/settings.json 파일을 생성하거나 편집합니다:

{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"editor.formatOnSave": true,
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
}
},
"python.analysis.typeCheckingMode": "basic",
"python.analysis.autoImportCompletions": true,
"editor.rulers": [88],
"files.trimTrailingWhitespace": true
}

2. PyCharm 설정

PyCharm은 JetBrains에서 만든 파이썬 전용 IDE로, 풍부한 기능을 제공합니다.

Community vs Professional

구분Community (무료)Professional (유료)
기본 Python 지원
디버거
가상환경 관리
웹 프레임워크 지원✅ (Django, FastAPI)
데이터베이스 도구
원격 개발
Jupyter 통합

학습용: Community 버전으로 충분합니다.

인터프리터 설정

File → Settings (macOS: PyCharm → Preferences)
→ Project: [프로젝트명]
→ Python Interpreter
→ Add Interpreter → Virtualenv Environment

3. Jupyter Notebook / Lab

Jupyter는 코드, 텍스트, 시각화를 하나의 문서에서 작성하는 인터랙티브 환경입니다. 데이터 분석과 교육에 특히 적합합니다.

설치 및 실행

# 가상환경 활성화 후 설치
pip install jupyter notebook jupyterlab

# Jupyter Notebook 실행
jupyter notebook

# JupyterLab 실행 (더 현대적인 UI)
jupyter lab

기본 사용법

# 셀 실행: Shift+Enter (다음 셀로 이동), Ctrl+Enter (현재 셀 유지)
# 새 셀 추가: A (위), B (아래)
# 셀 삭제: DD

# 코드 셀 예시
import pandas as pd
import matplotlib.pyplot as plt

data = {"이름": ["Alice", "Bob", "Charlie"], "점수": [95, 87, 92]}
df = pd.DataFrame(data)
print(df)

ipykernel 설치 (가상환경 연동)

# 가상환경 활성화 후
pip install ipykernel

# 가상환경을 Jupyter 커널로 등록
python -m ipykernel install --user --name=my-project --display-name "My Project (Python 3.12)"

# 등록된 커널 목록 확인
jupyter kernelspec list

마크다운 셀 활용

셀 타입을 Markdown으로 변경하면 수식, 텍스트, 표를 작성할 수 있습니다.

# 제목
## 수식 지원
$E = mc^2$

| 컬럼1 | 컬럼2 |
|-------|-------|
| 값1 | 값2 |

4. Python REPL 활용법

REPL(Read-Eval-Print Loop) 은 코드를 바로 실행하고 결과를 확인하는 인터랙티브 환경입니다.

기본 Python REPL

# REPL 실행
python3

# 종료
exit() 또는 Ctrl+D
# REPL에서 즉시 실험
>>> 2 + 3
5
>>> "hello".upper()
'HELLO'
>>> [x**2 for x in range(5)]
[0, 1, 4, 9, 16]

# 마지막 결과는 _ 변수에 저장됨
>>> 10 * 5
50
>>> _ + 1
51
>>> _ * 2
102

IPython (강화된 REPL)

pip install ipython

ipython
# IPython 특수 기능
In [1]: ?str.split # 문서 조회
In [2]: ??str.split # 소스코드 조회
In [3]: %timeit [x**2 for x in range(1000)] # 성능 측정
In [4]: %history # 명령 기록
In [5]: import os; os.getcwd()

# 탭 자동완성
In [6]: "hello". # 탭 누르면 메서드 목록 표시

멀티라인 입력

# 기본 REPL에서 멀티라인 코드 입력
>>> def fibonacci(n):
... if n <= 1:
... return n
... return fibonacci(n - 1) + fibonacci(n - 2)
...
>>> fibonacci(10)
55

5. 코드 품질 도구

black — 코드 포매터

pip install black

# 단일 파일 포맷
black script.py

# 디렉토리 전체 포맷
black src/

# 변경 내용만 확인 (실제 변경 없음)
black --check script.py
black --diff script.py
# black 적용 전
x = {'a':1,'b':2,'c':3}
def foo(a,b,c):
return a+b+c

# black 적용 후
x = {"a": 1, "b": 2, "c": 3}


def foo(a, b, c):
return a + b + c

ruff — 빠른 린터 (Rust 기반)

pip install ruff

# 린팅 실행
ruff check script.py

# 자동 수정
ruff check --fix script.py

# 포매팅 (black 대체 가능)
ruff format script.py

mypy — 정적 타입 검사

pip install mypy

# 타입 검사 실행
mypy script.py

# 엄격 모드
mypy --strict script.py
# mypy가 잡아주는 타입 오류 예시
def add(a: int, b: int) -> int:
return a + b

result = add("hello", 42) # mypy 오류: str 대신 int 필요

고수 팁: .editorconfig와 VS Code DevContainer

.editorconfig 파일로 에디터 설정을 팀 전체에 강제할 수 있습니다.

# .editorconfig (프로젝트 루트에 위치)
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.py]
indent_size = 4
max_line_length = 88

[*.json]
indent_size = 2

[*.yml]
indent_size = 2

VS Code DevContainer 로 팀 전체가 동일한 개발 환경을 공유합니다:

// .devcontainer/devcontainer.json
{
"name": "Python 3.12 Dev",
"image": "mcr.microsoft.com/devcontainers/python:3.12",
"features": {},
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.pylance",
"charliermarsh.ruff"
],
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python"
}
}
},
"postCreateCommand": "pip install -r requirements.txt"
}

이렇게 설정하면 팀원 누구나 동일한 파이썬 버전, 확장 프로그램, 설정으로 개발할 수 있습니다.


개발 환경이 구성되었다면 이제 첫 번째 파이썬 프로그램을 작성해볼 차례입니다. 다음 챕터에서 Hello, World!부터 시작하여 파이썬 코드의 기본 구조를 익혀보겠습니다.

Advertisement