728x90
반응형
Introduction
해당 글에서는 Poetry에 대해서 알아보도록 한다.
우선, Poetry 문서에서는 이렇게 소개한다.
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Poetry offers a lockfile to ensure repeatable installs, and can build your project for distribution.
- Poetry는 파이썬에서 종속성 관리와 패키징을 위한 도구입니다.
- 이를 통해 프로젝트가 의존하는 라이브러리를 선언할 수 있으며, 라이브러리를 관리(설치/업데이트) 해줍니다.
- Poetry는 반복 가능한 설치를 보장하는 잠금 파일을 제공하며, 배포를 위한 프로젝트를 구축할 수 있습니다.
System Requirements
Poetry requires Python 3.9+. It is multi-platform and the goal is to make it work equally well on Linux, macOS and Windows.
- Poetry는 파이썬 버전 3.9이상을 필요로 한다. 멀티플랫폼으로 리눅스, mac, 윈도우 등에서 동등하게 작동한다.
Installation
설치 방법은 아래와 같다.
- Linux, nacOS, Windows (WSL)
curl -sSL https://install.python-poetry.org | python3 -
- Windows (Powershell)
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
설치가 다 되었다면, 다음 명령어를 통해, 설치 확인을 해준다.
poetry --version
제대로 설치가 되면 아래와 같이 확인할 수 있다.
만약 확인이 안된다면, 환경 변수의 문제일 가능이 높다. 환경 변수 설정을 해준다. Windows의 경우, 시스템 환경변수 편집에서 추가해준다. (%APPDATA%\Python\Script)
그리고 다음 명령어를 통해, Poetry 업데이트를 할 수 있다.
poetry self update
Poetry 주요 명령어 정리
################################################
## 프로젝트 생성
# 새 Python 프로젝트 생성
poetry new my-package
# pyproject.toml 대화식 생성
poetry init
################################################
## 의존성 관리
# 의존성 설치
poetry install
# 패키지 추가
poetry add requests
# 패키지 제거
poetry remove requests
# 의존성 업데이트
poetry update # 전체 업데이트
poetry update requests # 특정 패키지만
################################################
## 빌드/배포
# 패키지 빌드
poetry build
# 패키지 배포
poetry publish
poetry publish --build # 빌드 후 배포
################################################
## 유틸리티
# 가상환경에서 명령 실행
poetry run python script.py
# 가상환경 셀 진입
poetry shell
# 설정 관리
poetry config --list
# pyproject.toml 검증
poetry check
################################################
## 환경 관리
# 가상환경 정보 조회
poetry env info
# 프로젝트의 가상환경 목록
poetry env list
# 가상환경 삭제
poetry env remove <python>
# 사용할 Python 버전 지정
poetry env use python3.9
################################################
## 캐시 관리
# 캐시 목록 조회
poetry cache list
# 캐시 삭제
poetry cache clear PyPI --all # 전체 캐시
poetry cache clear pypi:requests:2.24.0 # 특정 패키지
VS Code 인식 문제
poetry 설치를 했지만, VS Code에서 가상환경이 잡히지 않는 경우가 있었다. 이는 poetry에서 virtualenv 환경을 프로젝트 내부가 아닌 홈 디렉토리에 구축된 상황이다. 이에 대한 해결법은 이를 프로젝트 내부로 변경해주면 해결 된다. 변경한 후에 vscode를 재시작하면 알아서 ./.venv/bin/python 을 인터프리터로 인식하고 원하는대로 동작한다.
poetry config virtualenvs.in-project true
poetry config virtualenvs.path "./venv"
# 프로젝트 내부에 venv 새로 설치
poetry install && poetry update
Reference
728x90
반응형