더 나은 파이썬 개발 환경 구축Building a Better Python Development Environment
pyenv, poetry를 활용한 개발 환경 구축Setting up a development environment with pyenv and poetry
파이썬 개발시 불편한 것들
파이썬으로 처음 개발을 해보면 쉽다! 라고 많이 이야기 합니다. 하지만 실제로 어느 정도 개발을 진행하다보면 언어가 쉽게 느껴지는거지, 개발 환경을 구축하는게 다른 언어에 비해 불편한점이 많습니다.
- 파이썬 버전 관리
- pip 사용시 패키지 관리
- 가상환경 세팅
Windows에서는 동작하지 않는 패키지도 있기에 반드시 Windows 자체에서 파이썬으로 개발하지 마시고, WSL을 사용하시길 바랍니다.
더 나은 파이썬 환경 구축 방법에 대해서 알아보겠습니다.
파이썬 버전 관리 문제점
파이썬을 사용하다보면 “성능이 다른 언어에 비해 떨어진다.” 라는 말을 많이 듣습니다. 그럼 그 때마다 이런말에 혹합니다.
note : 이번 새로운 버전 3.XX 에서는 몇 퍼센트 성능 향상이…
나 : “ㅇㅋ 당장 파이썬 버전 올리러 간다!”
하지만 실제로 그렇게 하면…
- 이전 버전에서는 동작하던 패키지가 동작하지 않음.
- 어떤 서버는 파이썬 버전
3.8어떤 서버는3.12… - 시스템 기본 설치 되어있는 파이썬 버전 올리는 것도 잘 안돼.

> 파이썬 버전 관리는 생각보다 빡세다.
파이썬 버전 관리 : pyenv
파이썬을 직접 여러 버전을 설치하고 관리하는 것은 매우 불편하고 빡셉니다. 제가 추천하는 방법은 pyenv입니다. pyenv는 여러 버전의 파이썬 관리하고 설치할 수 있는 도구입니다. 다양한 프로젝트에서 필요한 버전을 손쉽게 전환할 수 있게 해줍니다.
설치 방법은 간단합니다. penv 깃허브 링크를 보시고 따라하시면 됩니다.
1. 설치
brew 또는 curl을 통해 설치가 가능합니다.
$ curl https://pyenv.run | bash
2. 쉘 설정
설치 후에는 쉘 설정 파일(~/.bashrc, ~/.zshrc 등)에 다음 줄을 입력해줍니다.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
3. 설치 확인
설치가 끝나면 쉘을 재 시작하고 제대로 설치가 됐는지 확인합니다.
$ pyenv --version
4. 특정 버전 설치
설치가 됐으니 이제 설치가 가능한 파이썬 버전을 확인하고 특정 버전을 설치합니다.
$ pyenv install --list
$ pyenv install 3.8.10
5. 버전 지정하기
global 옵션으로 시스템 전체에서 사용할 기본 버전을 설정합니다. 반대로 local로 특정 디렉토리에서 사용할 버전을 설정할 수 있습니다.
$ pyenv global 3.8.10
$ cd my_project
$ pyenv local 3.7.9
6. 사용가능한 버전 확인
현재 설치된 파이썬 버전 목록을 확인 할 수 있습니다.
$ pyenv versions
pip 패키지 관리의 어려움
pip는 2008년에 처음 만들어졌습니다. “package installer for Python” 약자로, 파이썬 패키지 관리자로써 표준 라이브러리의 일부로 포함되어 있습니다.
처음에 pip를 이용해 필요한 패키지를 땡겨오면 굉장히 편리하다. 하지만 해당 패키지에 의존성이 있는 패키지까지 같이 딸려오면서 문제가 생기기 시작합니다.
- 이 패키지가 어디서 딸려온거지? 이거 지워도 되나?
- 이거는 다른 패키지에도 쓰는데 버전이 안 맞는데?
- 내가 설치한 패키지가 뭐였지?
pip는 편리하지만 점점 설치하는 패키지가 많아질수록 실제로 관리가 전혀 안돼는 상태라고 생각하면 됩니다. 패키지를 설치하고 requirements로 관리가 안될때가 많습니다. 언제부터 무슨 패키지가 어디서 어떻게 빠져있는지도 모르는 경우가 많습니다.
패키지 관리 : Poetry
pip의 패키지 관리 문제를 해결하기 위해서 여러가지 방안이 있지만, 제가 써본것 중에서는 Poetry를 가장 추천합니다.
Poetry는 파이썬 패키지 및 프로젝트 관리를 위한 도구로, 종속성 관리를 쉽게 하고 프로젝트 환경을 더 효율적으로 설정할 수 있도록 도와줍니다. Poetry는 종속성 설치, 프로젝트 패키징, 배포 등을 포함한 여러 기능을 제공합니다.
설치 방법은 간단합니다. Poetry 설치 페이지를 보시고 따라하시면 됩니다.
1.pipx 설치
pipx는 파이썬 애플리케이션을 격리된 가상 환경에서 설치하고 실행할 수 있게 해주는 도구입니다. pipx 설치를 진행합니다.
$ sudo apt update
$ sudo apt install pipx
$ pipx ensurepath
$ sudo pipx ensurepath --global
2.pipx 로 Poetry 설치
아래의 명령으로 Poetry를 설치합니다.
$ pipx install poetry
3. Poetry 설치 확인
Poetry가 제대로 설치됐는지 확인합니다.
$ poetry --version
4. Poetry로 프로젝트 초기화
Poetry를 사용할 프로젝트 폴더로 이동해서 세팅해줍니다. init 명령을 사용해서 Poetry가 사용하는 pyproject.toml 파일이 생성을 할 수 있습니다. Poetry가 설치되어 있다면 해당 파일을 공유하여 동일한 환경을 손쉽게 구축할 수 있습니다.
-
-n: 입력 정보 생략 -
--python: 파이썬 버전 정보 입력. SemVer(Semantic Versioning) 규칙을 따름.
$ poetry init -n --python=^3.12
5. 패키지 추가, 삭제
add 명령을 통해 패키지를 추가 할 수 있습니다. 추가된 패키지는 pyproject.toml 파일에 기록됩니다. remove 명령으로 패키지를 삭제할 수 있습니다.
패키지를 설치할때 그룹을 지정 할수 있습니다. 지정한 그룹만 설치하거나 그룹만 제외하고 설치하는 등 유용하게 사용 할 수 있습니다.
$ poetry add fastapi
$ poetry remove fastapi
$ poetry add mypy -G dev
6. Poetry 패키지 설치
install 명령을 통해 pyproject.toml 파일에 기록된 모든 패키지를 설치합니다. 그룹에 상관이 전체가 설치됩니다. 특정 그룹만 또는 제외하고 설치 할 수도 있습니다.
$ poetry install
$ poetry install --with dev
$ poetry install --without test,docs
7. 가상환경(.venv) 생성 위치
기본 설정으로 패키지를 설치하게 되면 가상환경이 Poetry가 관리하는 기본 폴더에 각 가상환경(.venv)가 생성됩니다.
프로젝트 내에 가상환경을 생성하게 할 수도 있습니다. 아래의 옵션을 통해 pyproject.toml 파일이 존재하는 프로젝트 내에서 가상 환경(.venv)이 만들어지게 세팅하는 옵션을 활성화 시킬 수 있습니다.
$ poetry config virtualenvs.in-project true
마무리
파이썬은 여러 버전을 각 프로젝트에 맞게 사용해야 경우가 많습니다. 그런 경우 pyenv를 사용하여 여러 파이썬 버전을 손쉽게 관리 할 수 있습니다.
pip는 프로젝트가 커질수록 패키지 관리가 어렵기 때문에 Poetry를 사용하여 패키지를 명확히 관리하고 가상환경까지 편리하게 구성할 수 있습니다.
여기까지 좀 더 나은 파이썬 개발 환경을 구축하는 방법에 대해 알아보았습니다! 이것보다 각 상황에 알맞은 관리 도구들이 많이 있으니, 자신에게 맞는 파이썬 개발 환경을 구축하는 방법을 찾아 보시길 바랍니다. 😊
The annoying parts of Python development
People who try Python for the first time often say it’s easy! But once you get a fair way into real development, you realize it’s the language that feels easy; setting up the development environment has a lot more pain points than in other languages.
- Managing Python versions
- Managing packages with pip
- Setting up virtual environments
Some packages don’t work on Windows, so please don’t develop in Python on Windows itself. Use WSL instead.
Let’s look at how to build a better Python environment.
The trouble with managing Python versions
If you use Python, you hear “its performance lags behind other languages” a lot. And every time, lines like this reel you in:
Release notes: The new version 3.XX brings a performance boost of X percent…
Me: “OK, upgrading Python right now!”
But when you actually do that…
- Packages that worked on the previous version stop working.
- One server runs Python
3.8, another runs3.12… - Even upgrading the Python version that ships with the system doesn’t go smoothly.

> Managing Python versions is tougher than you think.
Python version management: pyenv
Installing and managing multiple Python versions by hand is a real hassle. My recommendation is pyenv. pyenv is a tool for installing and managing multiple versions of Python, and it lets you switch easily between the versions different projects need.
Installing it is simple. Just follow the pyenv GitHub page.
1. Install
You can install it with brew or curl.
$ curl https://pyenv.run | bash
2. Configure the shell
After installing, add the following lines to your shell config file (~/.bashrc, ~/.zshrc, etc.).
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
3. Verify the installation
Once it’s installed, restart your shell and check that it installed correctly.
$ pyenv --version
4. Install a specific version
Now that it’s installed, check which Python versions are available and install a specific one.
$ pyenv install --list
$ pyenv install 3.8.10
5. Set the version
The global option sets the default version for the whole system. Conversely, local sets the version for a specific directory.
$ pyenv global 3.8.10
$ cd my_project
$ pyenv local 3.7.9
6. List available versions
You can see the list of Python versions currently installed.
$ pyenv versions
The pain of managing packages with pip
pip was first created in 2008. Short for “package installer for Python,” it’s the Python package manager and is included as part of the standard library.
At first, pulling in the packages you need with pip is really convenient. But problems start once the packages those packages depend on get dragged in along with them.
- Where did this package come from? Can I delete it?
- Another package uses this one too, but the versions don’t match?
- Which packages did I actually install?
pip is convenient, but as the number of installed packages grows, you can basically consider them unmanaged. Often you install packages and they never make it into requirements. Many times you don’t even know since when, which package, went missing from where, and how.
Package management: Poetry
There are several ways to solve pip’s package management problems, but of the ones I’ve tried, I recommend Poetry the most.
Poetry is a tool for managing Python packages and projects. It makes dependency management easy and helps you set up project environments more efficiently. Poetry offers a range of features, including installing dependencies, packaging projects, and publishing.
Installing it is simple. Just follow the Poetry installation page.
1. Install pipx
pipx is a tool that lets you install and run Python applications in isolated virtual environments. Install pipx.
$ sudo apt update
$ sudo apt install pipx
$ pipx ensurepath
$ sudo pipx ensurepath --global
2. Install Poetry with pipx
Install Poetry with the command below.
$ pipx install poetry
3. Verify the Poetry installation
Check that Poetry installed correctly.
$ poetry --version
4. Initialize a project with Poetry
Go to the project folder where you want to use Poetry and set it up. The init command creates the pyproject.toml file that Poetry uses. Anyone with Poetry installed can use that file to easily reproduce the same environment.
-
-n: skip the interactive prompts -
--python: specify the Python version, following the SemVer (Semantic Versioning) rules.
$ poetry init -n --python=^3.12
5. Add and remove packages
The add command adds a package. Added packages are recorded in the pyproject.toml file. The remove command removes a package.
You can assign a group when installing a package. This is handy for things like installing only certain groups, or installing everything except certain groups.
$ poetry add fastapi
$ poetry remove fastapi
$ poetry add mypy -G dev
6. Install Poetry packages
The install command installs every package recorded in the pyproject.toml file, regardless of group. You can also install only specific groups, or exclude some.
$ poetry install
$ poetry install --with dev
$ poetry install --without test,docs
7. Where the virtual environment (.venv) is created
With the default settings, installing packages creates each virtual environment (.venv) in a default folder managed by Poetry.
You can also have the virtual environment created inside the project. The option below makes the virtual environment (.venv) get created inside the project where the pyproject.toml file lives.
$ poetry config virtualenvs.in-project true
Wrapping up
With Python, you often need to use different versions for different projects. In that case, pyenv makes it easy to manage multiple Python versions.
Package management with pip gets harder as a project grows, so use Poetry to manage packages clearly and set up virtual environments with ease.
That’s how to build a better Python development environment! There are plenty of other management tools suited to different situations, so find the way of setting up a Python development environment that works for you. 😊