poetry 로 파이썬 패키지 관리하기 Python Package Management with Poetry
파이썬으로 개발하다보면 여러가지 가상환경 매니징 패키지를 마주하게 된다. 이번에는 패키지의 버전까지 관리 해 주는 새로운 친구인 poetry를 도입 해 보려고 한다.
Poetry를 사용하자
우선 사용하기 위해서는 Poetry를 설치하자
설치
poetry는 파이썬 패키지로도 설치 할 수 있고, 맥의 설치패키지인 brew로도 설치 할 수 있다.
설치 방법 - brew
brew install poetry
설치 방법 - pip
pip install poetry
패키지 Init
poetry를 사용해서 파이썬을 개발 하려면 init 이라는 명령어로 해당 파일에 선언 해 주어야 한다.
poetry init
그럼, init에 필요한 여러가지들을 물어보는데 해당하는 파이썬 프로젝트의 특성에 맞게 적어주면 된다
여러가지를 물어보면서 파이썬 개발환경 세팅을 준비합니다
이렇게 적어주기는 했지만, 맨 첫번째 화면부터 마지막까지 그냥 엔터만 눌러주면 파이썬 개발 프로젝트 세팅이 마무리가 됩니다. 마무리가 되면 아래의 화면처럼 프로젝트 환경 요약본이 나오게 됩니다.
그럼 pyproject.toml 파일이 생성되면서 환경설정이 종료가 됩니다
poetry 사용법 - shell, add
poetry shell
poetry로 패키지 설정을 해 주었으니까 이제 사용 해 보겠습니다
가상환경을 실행시키는 shell 명령어를 사용 하겠습니다
poetry shell
그럼 패키지에 해당하는 파이썬 가상환경 생성 및 실행이 됩니다
poetry add
가상환경을 실행시키고 가상환경에 패키지를 설치 하려면 add 명령어를 실행 해야 합니다
poetry가 좋은 이유 중 하나는, 굳이 가상환경을 실행(shell)을 하지 않아도 add 명령어를 사용하면 알아서 설치 해 준다는 겁니다….!!!!!!
파이썬에서 자주 사용하는 Django, Flask, requests, aiohttp를 설치 해 보겠습니다
poetry add Django Flask requests aiohttp
원래 pip이면 그냥 해당하는 패키지의 최신 버전을 설치 해 주겠지만 우리친구 강한친구 poetry는 그러지 않습니다
받아야 할 패키지 파일들을 검색하고 해당하는 패키지들의 Dependency에 맞게 설치를 해 줍니다
pyproject.toml
poetry로 패키지들을 설치 한 후, 어떤 패키지가 설치 되었는지 자동으로 업데이트가 됩니다

When developing with Python, you encounter various virtual environment management packages. This time, I’m going to introduce poetry, a new friend that even manages package versions.
Let’s Use Poetry
First, let’s install Poetry to use it
Installation
Poetry can be installed as a Python package or through brew, Mac’s package installer.
Installation Method - brew
brew install poetry
Installation Method - pip
pip install poetry
Package Init
To develop Python using poetry, you need to declare it in the file using the init command.
poetry init
Then, it asks various things needed for init, which you can fill in according to your Python project’s characteristics
It prepares the Python development environment while asking various questions
Although I filled these in, from the first screen to the last, you can just press enter and the Python development project setup will be complete. When finished, you’ll see a project environment summary like the screen below.
Then the pyproject.toml file is created and the configuration is complete
How to Use Poetry - shell, add
poetry shell
Now that we’ve configured packages with poetry, let’s try using it
We’ll use the shell command to activate the virtual environment
poetry shell
Then the Python virtual environment corresponding to the package is created and activated
poetry add
To activate the virtual environment and install packages in it, you need to execute the add command
One of the reasons poetry is great is that you don’t even need to activate the virtual environment (shell) - using the add command will install packages automatically….!!!!!!
Let’s install Django, Flask, requests, and aiohttp which are frequently used in Python
poetry add Django Flask requests aiohttp
Normally pip would just install the latest version of the packages, but our strong friend poetry doesn’t do that
It searches for the package files to download and installs them according to each package’s dependencies
pyproject.toml
After installing packages with poetry, it automatically updates which packages have been installed

댓글남기기