ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • pyenv + poetry 조합으로 다양한 python 버전 개발환경 구성
    Python 2022. 6. 26. 14:24

    오랜만에 글을 쓰게 됐네요. 다른 언어도 마찬가지이겠지만 파이썬으로 개발을 할 때 두 가지 경우가 있습니다. 

     

    1. 처음부터 프로젝트를 시작하거나

    2. 이미 진행되고 있는 프로젝트에 참여하거나

     

    첫 번째 경우라면 poetry init 명령어를 통해 현재 설치돼있는 파이썬 버전으로 시작해도 되지만 기존 프로젝트에 참여하게 된다면 해당 프로젝트가 어떤 파이썬 버전으로 개발됐는지에 따라 나의 개발환경도 맞춰주어야 합니다. 버전에 따라 https://python.org 에서 맞는 버전을 다운로드하여 설치해도 되지만 엄청 귀찮은 일이 될 것입니다. 당장 여러분의 터미널 화면에서 python 또는 python3라고 쳐보세요. 설치된 환경에 맞는 파이썬 버전이 실행될 것입니다. 간단하게 원하는 파이썬 버전을 설치하고 실행하고 싶으면 어떻게 할까요? pyenv 라는 것을 사용하면 이 모든 것이 쉬워집니다.

     

    이번 글에서는 pyenv 설치와 사용법, poetry 사용 시 원하는 python 버전 선택하는 방법에 대해 포스팅하려고 합니다.

     

    pyenv 설치하기

    pyenv는 파이썬 버전 관리 툴이고 https://github.com/pyenv/pyenv 개발된 소스는 여기 있습니다. Ruby의 버전 관리 툴인 rbenv를 포크해서 만들었다고 하네요. 

    https://github.com/pyenv/pyenv#installation

     

    GitHub - pyenv/pyenv: Simple Python version management

    Simple Python version management. Contribute to pyenv/pyenv development by creating an account on GitHub.

    github.com

     

    여기에 설치 방법이 있습니다. 공식적으로 Homebrew를 통한 macOS만 지원하고 윈도우의 경우 WSL에 설치를 하거나 윈도우 네이티브에서 사용하려면 pyenv-win을 참고하시면 되겠습니다. 설치가 잘 됐다면 아래와 같이 pyenv --help로 사용법을 확인해보실 수 있습니다.

    ➜  ~ pyenv --help
    Usage: pyenv <command> [<args>]
    
    Some useful pyenv commands are:
       --version   Display the version of pyenv
       activate    Activate virtual environment
       commands    List all available pyenv commands
       deactivate   Deactivate virtual environment
       exec        Run an executable with the selected Python version
       global      Set or show the global Python version(s)
       help        Display help for a command
       hooks       List hook scripts for a given pyenv command
       init        Configure the shell environment for pyenv
       install     Install a Python version using python-build
       local       Set or show the local application-specific Python version(s)
       prefix      Display prefixes for Python versions
       rehash      Rehash pyenv shims (run this after installing executables)
       root        Display the root directory where versions and shims are kept
       shell       Set or show the shell-specific Python version
       shims       List existing pyenv shims
       uninstall   Uninstall a specific Python version
       version     Show the current Python version(s) and its origin
       version-file   Detect the file that sets the current pyenv version
       version-name   Show the current Python version
       version-origin   Explain how the current Python version is set
       versions    List all Python versions available to pyenv
       virtualenv   Create a Python virtualenv using the pyenv-virtualenv plugin
       virtualenv-delete   Uninstall a specific Python virtualenv
       virtualenv-init   Configure the shell environment for pyenv-virtualenv
       virtualenv-prefix   Display real_prefix for a Python virtualenv version
       virtualenvs   List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
       whence      List all Python versions that contain the given executable
       which       Display the full path to an executable
    
    See `pyenv help <command>' for information on a specific command.
    For full documentation, see: https://github.com/pyenv/pyenv#readme

     

    특정 버전 설치

    이제 특정 버전을 설치하고 사용해보도록 하겠습니다.

     

    기본적으로 pyenv install {{설치할 버전}} 이렇게 입력하시면 됩니다. 예를 들어 3.10.4 버전을 설치하고 싶으면 pyenv install 3.10.4 이렇게 입력하면 됩니다. 그런데 내가 설치할 버전이 pyenv에서 지원하는지 보려면 어떻게 할까요?

    ➜  ~ pyenv install --list
    Available versions:
      2.1.3
      2.2.3
      2.3.7
      2.4.0
      2.4.1
      2.4.2
      2.4.3
      2.4.4
      2.4.5
      2.4.6
      2.5.0
      2.5.1
      ...(생략)
      3.8.8
      3.8.9
      3.8.10
      3.8.11
      3.8.12
      3.8.13
      3.9.0
      3.9-dev
      3.9.1
      3.9.2
      3.9.4
      3.9.5
      3.9.6
      3.9.7
      3.9.8
      3.9.9
      3.9.10
      3.9.11
      3.9.12
      3.10.0
      3.10-dev
      3.10.1
      3.10.2
      ...

    pyenv install --list 라는 명령어를 통해 확인할 수 있는데 설치 가능한 모든 버전 출력되기 때문에 3.9 버전이 무엇이 있나 보려면 아래처럼 필터링해서 보실 수도 있습니다.

    ➜  ~ pyenv install --list | grep 3.9
      3.9.0
      3.9-dev
      3.9.1
      3.9.2
      3.9.4
      3.9.5
      3.9.6
      3.9.7
      3.9.8
      3.9.9
      3.9.10
      3.9.11
      3.9.12
      miniconda-3.9.1
      miniconda3-3.9.1
      miniconda3-3.9-4.9.2
      miniconda3-3.9-4.10.3
      miniconda3-3.9-4.11.0
      nogil-3.9.10
      pypy2.7-7.3.9-src
      pypy2.7-7.3.9
      pypy3.7-7.3.9-src
      pypy3.7-7.3.9
      pypy3.8-7.3.9-src
      pypy3.8-7.3.9
      pypy3.9-7.3.8-src
      pypy3.9-7.3.8
      pypy3.9-7.3.9-src
      pypy3.9-7.3.9

    저기에 출력된 대로 설치하고 싶은 버전을 pyenv install 로 설치하시고 설치가 완료되었다면 해당 버전을 사용하게 끔 환경설정을 해야 합니다.

     

    pyenv global {{사용할 버전}} 이라는 명령어로 사용할 버전을 선택할 수 있습니다. 물론 사용할 버전은 설치되어있어야 합니다. 아래 화면처럼 설정된 버전에 따라 인터프리터 버전이 변경되는 것을 보실 수 있습니다.

     

    pyenv로 poetry가 사용하는 파이썬 버전 바꾸기

    ➜  poetry-version-test python -c "import sys; print(sys.version)"
    3.8.6 (default, Dec 15 2020, 11:25:35)
    [Clang 12.0.0 (clang-1200.0.32.28)]
    ➜  poetry-version-test poetry init
    
    This command will guide you through creating your pyproject.toml config.
    
    Package name [poetry-version-test]:
    Version [0.1.0]:
    Description []:
    Author [Son Hyoungwoo <hwoo.son@navercorp.com>, n to skip]:
    License []:
    Compatible Python versions [^3.8]:
    
    Would you like to define your main dependencies interactively? (yes/no) [yes]
    You can specify a package in the following forms:
      - A single name (requests)
      - A name and a constraint (requests@^2.23.0)
      - A git url (git+https://github.com/python-poetry/poetry.git)
      - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)
      - A file path (../my-package/my-package.whl)
      - A directory (../my-package/)
      - A url (https://example.com/packages/my-package-0.1.0.tar.gz)
    
    Search for package to add (or leave blank to continue):
    
    Would you like to define your development dependencies interactively? (yes/no) [yes]
    Search for package to add (or leave blank to continue):
    
    Generated file
    
    [tool.poetry]
    name = "poetry-version-test"
    version = "0.1.0"
    description = ""
    authors = ["Son Hyoungwoo <hwooson12@gmail.com>"]
    
    [tool.poetry.dependencies]
    python = "^3.8"
    
    [tool.poetry.dev-dependencies]
    
    [build-system]
    requires = ["poetry-core>=1.0.0"]
    build-backend = "poetry.core.masonry.api"
    
    
    Do you confirm generation? (yes/no) [yes]

    poetry init을 하면 프로젝트 설정파일(pyproject.toml)을 생성하는데 중간에 Compatible Python versions라는 질문이 나옵니다. 따로 입력하지 않으면 현재 설정된 파이썬 버전을 사용하게 됩니다. pyproject.toml 파일이 생성된 후에 poetry shell 이라는 명령어를 사용하면 poetry가 알아서 가상 환경을 만들어주면서 가상 환경으로 진입하게 됩니다. pyenv env list 를 통해 생성된 가상 환경 목록을 볼 수 있습니다.

     

    만약에 사용하는 파이썬 버전을 변경하고 싶을 때 어떻게 하면 될까요? 

     

    현재 python 인터프리터 버전을 pyenv를 통해 변경하시고 poetry env use python 을 입력하시면 새로운 버전의 가상 환경이 아래와 같이 생성됩니다.

    기존 env를 지우고 싶으시다면 poetry env remove {{버전명}} 을 입력하시면 됩니다.

     

    지금까지 pyenv를 통해 poetry가 생성하는 가상 환경의 파이썬 버전 변경 방법에 관해 알아보았습니다. 개발환경을 세팅하는데 많은 도움이 되셨길 바랍니다.

    반응형

    댓글

Designed by Tistory.