Python周辺の管理にpoetryを試用してみました

Pythonロゴ

Python周辺の管理はpipenvを使っていて、特に不満とかあるわけないのですが、

poetryを使ってみたくなったので、Mint Linux上にpoetryの環境を構築し試用してみる事にしました。

以下の順でインストールします

  1. asdf, python
  2. poetry
cat /etc/os-release

NAME="Linux Mint"
VERSION="20.3 (Una)"
ID=linuxmint
ID_LIKE=ubuntu
PRETTY_NAME="Linux Mint 20.3"
VERSION_ID="20.3"
HOME_URL="https://www.linuxmint.com/"
SUPPORT_URL="https://forums.linuxmint.com/"
BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/"
PRIVACY_POLICY_URL="https://www.linuxmint.com/"
VERSION_CODENAME=una
UBUNTU_CODENAME=focal

参考

開発環境のインストール

(必要であるなら)Linux Mintへインストールします。

sudo apt-get update;
sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

私の場合、上記パッケージをインストールしなければこんなエラーがでました。

asdf install python 3.10.4

python-build 3.10.4 /home/toshiya/.asdf/installs/python/3.10.4
Downloading Python-3.10.4.tar.xz...
-> https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
Installing Python-3.10.4...

BUILD FAILED (Linuxmint 20.3 using python-build 2.2.5-11-gf0f2cdd1)

Inspect or clean up the working tree at /tmp/python-build.20220410091243.23288
Results logged to /tmp/python-build.20220410091243.23288.log

Last 10 log lines:
  File "/tmp/python-build.20220410091243.23288/Python-3.10.4/Lib/ensurepip/__init__.py", line 277, in _main
    return _bootstrap(
  File "/tmp/python-build.20220410091243.23288/Python-3.10.4/Lib/ensurepip/__init__.py", line 193, in _bootstrap
    return _run_pip([*args, *_PACKAGE_NAMES], additional_paths)
  File "/tmp/python-build.20220410091243.23288/Python-3.10.4/Lib/ensurepip/__init__.py", line 93, in _run_pip
    return subprocess.run([sys.executable, '-W', 'ignore::DeprecationWarning',
  File "/tmp/python-build.20220410091243.23288/Python-3.10.4/Lib/subprocess.py", line 524, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/tmp/python-build.20220410091243.23288/Python-3.10.4/python', '-W', 'ignore::DeprecationWarning', '-c', '\nimport runpy\nimport sys\nsys.path = [\'/tmp/tmpzq8fakg5/setuptools-58.1.0-py3-none-any.whl\', \'/tmp/tmpzq8fakg5/pip-22.0.4-py3-none-any.whl\'] + sys.path\nsys.argv[1:] = [\'install\', \'--no-cache-dir\', \'--no-index\', \'--find-links\', \'/tmp/tmpzq8fakg5\', \'--root\', \'/\', \'--upgrade\', \'setuptools\', \'pip\']\nrunpy.run_module("pip", run_name="__main__", alter_sys=True)\n']' returned non-zero exit status 1.
make: *** [Makefile:1280: install] エラー 1

asdfのインストール

プロジェクト用のバージョン管理ツール、という事でこちらもインストールしていきます。

Install

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.9.0
echo ". $HOME/.asdf/asdf.sh" >> ~/.bashrc
echo ". $HOME/.asdf/completions/asdf.bash" >> ~/.bashrc
source ~/.bashrc

Pythonプラグインを追加

Pythonのバージョン管理をする。

# asdf で python があるのか確認
asdf plugin-list-all | grep python

# python を追加
asdf plugin-add python

# python で使用可能なバージョンを確認
asdf list-all python

# python の任意のバージョンのみに絞って表示(list allに"-"ハイフンはあってもなくても良いみたい)
asdf list all python 3.9

# python をインストールして使用可能にするように整備
asdf install python 3.10.4
asdf global python 3.10.4
asdf local python 3.10.4  # 現在のフォルダ内のみ有効なバージョン変更
asdf reshim python  # Pythonの再設定
asdf current  # 現在のバージョンの確認
which python     # /home/toshiya/.asdf/installs/python/3.10.4/bin/python
python --version # Python 3.10.4
which pip        # /home/toshiya/.asdf/shims/pip
pip --version    # pip 22.0.4 from /home/toshiya/.asdf/installs/python/3.10.4/lib/python3.10/site-packages/pip (python 3.10)

インストール済みパッケージの確認、表示

asdf list

システム(asdf使用前)のPythonに戻す時

asdf local python system

アンインストール

asdf uninstall python 3.10.4

Poetry

ダウンロード、設定等をしていきます。

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -

有効にします。

source $HOME/.poetry/env

仮想環境をプロジェクト直下に作るように設定。
仮想環境がプロジェクトの.venv/配下に作成されるようになるので、VSCodeで実行環境を指定できるようになる、ということらしいです。

poetry config --list
poetry config virtualenvs.in-project true

使い方

こちらが簡単か!?

プロジェクトフォルダとその中にプロジェクトを作ってくれるので、最初っから作業する時はこちらが楽だと思いました。

poetry new project-name

1つずつやっていく方法

poetry init -n

pyproject.tomlというファイルが作られる。

ライブラリをインストールしてみる

requestsをインストールする。

poetry add requests

linterformatterをインストールする。

poetry add --dev flake8 yapf

flake8

(使い方)

poetry run flake8 /path/to/sorce.py

yapf

Google製のフォーマッタ

vim /Path/To/.style.yapf

[style]
based_on_style = google  # pep8とか
spaces_before_comment = 4
split_before_logical_operator = true

(使い方) cd /Path/To などしてから

poetry run yapf sorce.py  # 結果を表示
poetry run yapf -d sorce.py  # 修正を表示
poetry run yapf -i sorce.py  # 結果を上書き

pyproject.tomlを元にインストールをする方法

poetry install

jupyter

poetry add -D ipykernel      #カーネルが必要とのこと
sudo apt install python3-pip #
poetry add jupyterlab
poetry run jupyter lab

tip

アップデートがあるか確認

poetry show --outdated

アップデートの試し

poetry update --dry-run

Poetryをインストールしてみて

それでは今日はここまでとします。
pipenvよりも高機能ということで、使うのに(意識的な)慣れも必要かなという感じしました。
ですが、慣れればpoetryに移行していきたいな、と思うアプリでした。慣れていこうと思います。

以上になります

鹿児島県の出水市という所に住んでいまして、インターネット周辺で色々活動して行きたいと思ってるところです。 Webサイト作ったり、サーバ設定したり、プログラムしたりしている、釣りと木工好きなMacユーザです。 今はデータサイエンスに興味を持って競馬AI予想を頑張ってます。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

コメントする

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください