Poetry๋ Python์ ์์กด์ฑ ๊ด๋ฆฌ์ ํจํค์ง์ ์ํ ํ๋์ ์ธ ๋๊ตฌ๋ค. ์ด ๋ฌธ์์์๋ macOS ํ๊ฒฝ์์ Poetry๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ก์ ํธ ๋ด๋ถ์ ๊ฐ์ํ๊ฒฝ์ ์ค์ ํ๊ณ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃฌ๋ค.
Poetry ์ค์น
Homebrew๋ฅผ ์ฌ์ฉํ ์ค์น (๊ถ์ฅ)
brew install poetry๊ณต์ ์ค์น ์คํฌ๋ฆฝํธ ์ฌ์ฉ
curl -sSL https://install.python-poetry.org | python3 -์ค์น ํ์ธ
poetry --versionํ๋ก์ ํธ ์ด๊ธฐ ์ค์
1. ์ ํ๋ก์ ํธ ์์ฑ
poetry new my-project
cd my-project
# ๋๋ ๊ธฐ์กด ํ๋ก์ ํธ์์ ์ด๊ธฐํ
poetry init2. ๊ฐ์ํ๊ฒฝ ์ค์
ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ ๋ด๋ถ์ ๊ฐ์ํ๊ฒฝ์ ์์ฑํ๋ค.
# ๊ฐ์ํ๊ฒฝ์ ํ๋ก์ ํธ ๋ด๋ถ์ ์์ฑํ๋๋ก ์ค์
poetry config virtualenvs.in-project true
# ๊ฐ์ํ๊ฒฝ ๊ฒฝ๋ก ์ค์
poetry config virtualenvs.path "./.venv"
# ์์กด์ฑ ์ค์น ๋ฐ ์
๋ฐ์ดํธ
poetry install && poetry update์์กด์ฑ ๊ด๋ฆฌ
1. ํจํค์ง ์ถ๊ฐ ๋ฐ ์ ๊ฑฐ
# ํจํค์ง ์ถ๊ฐ
poetry add requests pandas numpy
# ๊ฐ๋ฐ ์์กด์ฑ์ผ๋ก ์ถ๊ฐ
poetry add pytest black isort --dev
# ํจํค์ง ์ ๊ฑฐ
poetry remove requests2. requirements.txt ๋ณํ
requirements.txt ์์ฑ
# ๊ธฐ๋ณธ ์์ฑ
poetry export --without-hashes --format=requirements.txt > requirements.txt
# ๊ฐ๋ฐ ์์กด์ฑ ํฌํจ
poetry export --without-hashes --format=requirements.txt --dev > requirements.txtrequirements.txt์์ ์์กด์ฑ ๊ฐ์ ธ์ค๊ธฐ
poetry add $(cat requirements.txt)VSCode ํตํฉ
1. Python ์ต์คํ ์ ์ค์น
Command + Shift + X๋ฅผ ๋๋ฌ ์ต์คํ ์ ๋ง์ผํ๋ ์ด์ค๋ฅผ ์ด๊ณ โPythonโ์ ์ค์นํ๋ค.
2. ํ๋ก์ ํธ ์ค์
.vscode/settings.json ํ์ผ์ ์์ฑํ๊ณ ๋ค์ ๋ด์ฉ์ ์ถ๊ฐํ๋ค:
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.formatting.provider": "black",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true
}3. ์ธํฐํ๋ฆฌํฐ ์ ํ
- Command + Shift + P๋ฅผ ๋๋ฌ ๋ช ๋ น ํ๋ ํธ๋ฅผ ์ฐ๋ค
- โPython: Select Interpreterโ ์ ๋ ฅ
.venv/bin/python์ ์ ํํ๋ค
์ ์ฉํ ํ
1. PATH ์ค์
Poetry๊ฐ ์ค์น๋์์ง๋ง ๋ช
๋ น์ด๊ฐ ์ธ์๋์ง ์๋ ๊ฒฝ์ฐ ~/.zshrc ๋๋ ~/.bash_profile์ ์ถ๊ฐ:
export PATH="$HOME/.local/bin:$PATH"2. pyenv์ ํจ๊ป ์ฌ์ฉ
macOS์์ ์ฌ๋ฌ Python ๋ฒ์ ์ ๊ด๋ฆฌํ๋ ค๋ฉด pyenv์ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค:
# pyenv ์ค์น
brew install pyenv
# Python ๋ฒ์ ์ค์น
pyenv install 3.9.9
# ํ๋ก์ ํธ์์ ์ฌ์ฉํ Python ๋ฒ์ ์ง์
pyenv local 3.9.9
# Poetry๊ฐ pyenv์ Python์ ์ฌ์ฉํ๋๋ก ์ค์
poetry env use $(pyenv which python)3. ์๋ ์์ฑ ์ค์
zsh์์ Poetry ๋ช ๋ น์ด ์๋ ์์ฑ์ ํ์ฑํ:
# ~/.zshrc์ ์ถ๊ฐ
poetry completions zsh > ~/.zfunc/_poetry
echo 'fpath+=~/.zfunc' >> ~/.zshrc
echo 'autoload -Uz compinit && compinit' >> ~/.zshrc๋ฌธ์ ํด๊ฒฐ
1. ๊ฐ์ํ๊ฒฝ ์ฌ์ค์
# ๊ฐ์ํ๊ฒฝ ์ญ์
poetry env remove python
# ์บ์ ์ ๋ฆฌ
poetry cache clear . --all
# ์์กด์ฑ ์ฌ์ค์น
poetry install2. ๊ถํ ๋ฌธ์ ํด๊ฒฐ
# Poetry ์บ์ ๋๋ ํ ๋ฆฌ ๊ถํ ์์
sudo chown -R $(whoami) ~/Library/Caches/pypoetry