poetry
Python packaging and dependency management made easy
What is Poetry?
Python packaging and dependency management made easy
python 프로젝트의 의존성 관리를 도와주는 툴이다. pip와는 다르게 .toml
, .lock
파일을 생성하여 의존성 관리를 도와준다.(poetry는 Python 3.8+.
을 지원합니다.)
pyproject.toml
: 프로젝트 의존성의 메타 데이터 저장(의존성들간의 충돌을 해결할 수 있게 도와준다.)poetry.lock
: 설치된 패키지들의 version, hash 저장(동일한 버전을 유지할 수 있게 도와준다)
Why Poetry?
poetry를 왜 사용하는지 dependency manager의 기능은 여러가지가 있지만 지금 글에서는 크게 두가지 관점에서 말하겠습니다.
dependency version resolving
한 프로젝트에 A, B 두개의 Dependency 의 설치를 필요로 하는 상황이 있다고 가정해보겠습니다.
A 는 some (= 1.0.1), B 는 some (>= 2.0.0) 처럼, A 와 B 둘다 some 이라는 패키지의 다른 버전을 요구할 때에, pip 20.3 이전 버전에서는 Dependency Resolving 을 지원하지 않았고, 20.3 이후 버전부터 지원하고 있습니다.
dependency locking
기존의 pip를 사용한다면 아래의 명령어로 직접 패키지들의 버전을 사용자가 입력 및 관리하여야합니다.
1 2
pip freeze > requirements.txt # 설치된 package list를 requirements.txt로 쓴다. pip install -r requirements.txt # install packages
그러나 poetry는 아래와 같이 dependency를 알아서 lock 파일을 생성하여 사용자가 따로 관리할 필요가 없습니다.
예시로 아래는
pendulum
package 설치 과정입니다.1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ poetry add pendulum Using version ^2.0.5 for pendulum Updating dependencies Resolving dependencies... (1.5s) Package operations: 4 installs, 0 updates, 0 removals - Installing six (1.13.0): Downloading... 25% - Updating pytzdata (2019.3 -> 2020.4): Installing... - Installing pendulum (2.0.5) Writing lock file
또한 poetry 를 통해 dependency 를 설치하면 poetry.lock
파일을 통해 Locking 을 해주고, 따로 virtualenv 를 통해 가상환경을 만들지 않아도, poetry 에 설정을 해놓으면 알아서 venv 를 만들어줍니다. 이러한 또한 pip나 conda에선 없던 build
와 publish
를 제공하여 배포 또한 쉽게 할 수 있습니다.
Start Poetry
자세한 사항은 poetry 공식 installer를 참조해주세요.
1. Install Poetry
poetry를 각 os 별로 설치한다.
- linux, macOS, window(WSL)
1
curl -sSL https://install.python-poetry.org | python3 -
- window(PowerShell)
1
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
- macOS with brew
1
brew install poetry
2. check version
poetry를 설치 후 버전을 확인한다.
1
poetry --version
3. Setup Poetry
poetry로 프로젝트를 만들어서 시작할 수도 있지만 이미 생성된 프로젝트에서 poetry를 사용하는것 또한 가능하다.
- initialize project
가성환경을 설정한다.1 2
poetry new {프로젝트명} poetry init # 이미 생성된 디렉토리에 시작할때
- activate virtual environment
가상환경을 실행한다.1
poetry shell
- isntall package
가상환경을 실행한다.1
poetry add {package} # ex) poetry add fastapi
Poetry Command
자주 사용하는 poetry 명령어를 적어놨다.
1
2
3
4
5
poetry install # install all packages
poetry add {package} # add some package ex) poetry add requests
poetry remove {package} # remove some packageex) poetry remove requests
poetry show # show installed all packages
poetry show --tree # 설치된 package들의 의존성까지 보여준다.
[출처]
https://python-poetry.org/