[Python] Version Manager & Poetry 설치, 사용하기
Mac 기준으로
Python Version Manager 설치
pyenv 설치
brew install pyenv
.zshrc 설정 추가
# pyenv setting
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Python Virtual Environment 설치
pyenv-virtualenv 설치
brew install pyenv-virtualenv
.zshrc 설정 추가
# pyenv-virtualenv setting
eval "$(pyenv virtualenv-init -)"
Python Version 설치
python 버전 목록 확인
pyenv install --list
python 특정 버전 설치
pyenv install 3.12.4
현재 사용버전
pyenv versions
* system (set by /Users/dowon2yun/.pyenv/version)
2.7.14
3.6.2
3.6.2/envs/mms
3.12.4
mms --> /Users/dowon2yun/.pyenv/versions/3.6.2/envs/mms
Python Version 적용
전체 사용 설정
- pyenv global [version]
pyenv global 3.12.4
pyenv versions
system
2.7.14
3.6.2
3.6.2/envs/mms
* 3.12.4 (set by /Users/dowon2yun/.pyenv/version)
로컬 프로젝트만 사용 설정
- pyenv local [version]
~ mkdir test
~ cd test
~/test pyenv local 3.6.2
~/test ls -alrt
total 8
drwxr-xr-x 3 dowon2yun staff 96 Jun 17 23:00 .
-rw-r--r-- 1 dowon2yun staff 6 Jun 17 23:00 .python-version
drwxr-xr-x+ 128 dowon2yun staff 4096 Jun 17 23:00 ..
~/test cat .python-version
3.6.2
현재 shell만 사용 설정
- pyenv shell [version]
~/test pyenv shell 3.12.4
~/test pyenv version
3.12.4 (set by PYENV_VERSION environment variable)
Python Virtual Env 설정
python 패키지의 버전 격리 환경을 만들어 준다.
python 버전과 virtual 환경 생성
- pyenv virtualenv [version] [virtual-name]
~ mkdir test2
~ cd test2
~/test2 pyenv virtualenv 3.12.4 venv
~/test2 pyenv versions
system
2.7.14
3.6.2
3.6.2/envs/mms
* 3.12.4 (set by PYENV_VERSION environment variable)
3.12.4/envs/venv
mms --> /Users/dowon2yun/.pyenv/versions/3.6.2/envs/mms
venv --> /Users/dowon2yun/.pyenv/versions/3.12.4/envs/venv
Python Virtual Env 활성화/비활성화
- pyenv activate [virtual-name]
~/test2 pyenv activate venv
(venv) ~/test2 pyenv versions
system
2.7.14
3.6.2
3.6.2/envs/mms
3.12.4
3.12.4/envs/venv
mms --> /Users/dowon2yun/.pyenv/versions/3.6.2/envs/mms
* venv --> /Users/dowon2yun/.pyenv/versions/3.12.4/envs/venv (set by PYENV_VERSION environment variable)
- pyenv deactivate [virtual-name]
(venv) ~/test2 pyenv deactivate venv
~/test2 pyenv versions
system
2.7.14
3.6.2
3.6.2/envs/mms
* 3.12.4 (set by /Users/dowon2yun/.pyenv/version)
3.12.4/envs/venv
mms --> /Users/dowon2yun/.pyenv/versions/3.6.2/envs/mms
venv --> /Users/dowon2yun/.pyenv/versions/3.12.4/envs/venv
~/test2
Poetry 설치
전문적인 의존성 관리 및 패키지 배포관리 툴인 poetry를 설치한다.
https://python-poetry.org/docs/
pipx 를 통해 설치힌다.
https://pipx.pypa.io/stable/installation/
brew install pipx
pipx ensurepath
sudo pipx ensurepath --global
pipx를 통해 poetry를 설치한다. python 3.12.3 을 사용한다는 메세지가 출력된다.
pipx install poetry
// result message
installed package poetry 1.8.3, installed using Python 3.12.3
These apps are now globally available
- poetry
poetry를 실행한다.
poetry
// result mesage
Poetry (version 1.8.3)
Poetry 다음 Tab으로 명령 목록 보기
oh-my-zsh 설정이 .zshrc 에 있음을 가정한다
// .zshrc 에서 ZSH_CUSTOM 주석 풀고 계정 폴더 밑으로 oh-my-zsh 설정
# Would you like to use another custom folder than $ZSH/custom?
ZSH_CUSTOM=/Users/peter/oh-my-zsh
// 저장후 변경 적용
. .zshrc
// 폴더 생성
mkdir $ZSH_CUSTOM/plugins/poetry
oh-my-zsh 의 plugins 에 poetry 추가
// .zshrc oh-my-zsh의 plugins 에 poetry 추가
plugins=(git poetry)
// .zshrc 변경 적용
. .zshrc
다음 명령 수행
poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetry
테스트 "peotry in" 까지 입력하고 tab key를 치면 아래와 같이 init, install 등의 poetry 명령 목록이 출력된다.
$ . .zshrc
$ poetry in
init -- Creates a basic pyproject.toml file in the current directory.
install -- Installs the project dependencies.
Poetry 통한 프로젝트, 패키지 추가
[1] Poetry 기반 프로젝트 생성
- poetry new [project-name]
poetry new ai-agent
[2] 프로젝트로 이동해서 가상환경을 프로제트내로 설정한다.
poetry config virtualenvs.in-project true
[3] poetry 프로젝트 가상환경으로 변경
poetry shell
[4] ai_agent 패키지 폴더에 __main__.py 추가
- 폴더 지정으로 run 하기 위해 __init__.py 위치에 __main__.py 파일을 추가한다.
- .env 파일 생성후 KEY 값 설정
- 명령
- poetry shell 미수행시 : poetry run python [folder-name] 또는 [file-name]
- poetry shell 수행시 : python [folder-name]
// .env 파일
OPENAI_API_KEY=sh-proj-xsdhfdrerjelrelreahahhahahaahaha
// __main__.py 내역
import os
from dotenv import load_dotenv
load_dotenv()
print(f"[API KEY]\n{os.environ['OPENAI_API_KEY']}")
// 실행
poetry run python ai_agent
[API KEY]
sh-proj-xsdhfdrerjelrelreahahhahahaahaha
[5] Python version 변경
1) pyenv install [newVersion]
2) poetry env remove [oldVersion]
3) poetry env use [newVersion]
References
https://python-poetry.org/docs/
Introduction | Documentation | Poetry - Python dependency management and packaging made easy
If you installed using the deprecated get-poetry.py script, you should remove the path it uses manually, e.g. rm -rf "${POETRY_HOME:-~/.poetry}" Also remove ~/.poetry/bin from your $PATH in your shell configuration, if it is present.
python-poetry.org
Poetry로 파이썬 의존성 관리하기
Poetry? 파이썬에서 사용하는 의존성 관리자는 몇 가지가 있습니다. 파이썬의 공식 의존성 관리자인 pip, 그리고 pip와 virtualenv를 같이 사용할 수 있는 Pipenv가 있고, 이번에 소개할 Poetry가 있습니다.
blog.flynnpark.dev
https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins
Plugins
🙃 A delightful community-driven (with 2,300+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, git, macOS, hub, docker, homebrew, node, php, pyth...
github.com
구성파일은 TOML 파일 형식
파이썬 앱 구성을 더 쉽게⋯TOML의 이해와 기본 활용
소프트웨어 개발에서 흥미로운 반전은 가장 간단한 의사 결정이 때로는 가장 어려운 의사 결정이 되기도 한다는 것이다. 예를 들면 애플리케이션이나
www.itworld.co.kr
https://teddylee777.github.io/poetry/poetry-tutorial/
poetry 의 거의 모든것 (튜토리얼)
poetry 로 가상환경을 구축하는 방법을 단계별로 설명합니다.
teddylee777.github.io