見出し画像

【IT】poetryの導入

皆さま
こんにちは

Djagoフレームワークで主に使っている
Pythonのパッケージ管理にpipを使っております。

今回は、別のパッケージ管理ツールの「poetry」
を導入します。

最初に、テスト用のユーザ(ユーザ名を'testpoetry'とします)
を作成し、pyenvを導入します。

$ sudo su -
[sudo] lmf のパスワード:
# useradd -m -s /bin/bash testpoetry
# passwd testpoetry
新しいパスワード:
新しいパスワードを再入力してください:
passwd: パスワードは正しく更新されました

# usermod -aG docker testpoetry
# usermod -aG sudo testpoetry
# id testpoetry
uid=1008(testpoetry) gid=1009(testpoetry) groups=1009(testpoetry),27(sudo),998(docker)
# su - testpoetry
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
# su - testpoetry

$ cd ~/
$ cp -p .bashrc .bashrc_orig
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ source ~/.bashrc

$ pyenv install 3.9.6
Downloading Python-3.9.6.tar.xz...
-> https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tar.xz
Installing Python-3.9.6...
patching file Misc/NEWS.d/next/Build/2021-10-11-16-27-38.bpo-45405.iSfdW5.rst
patching file configure
patching file configure.ac
Installed Python-3.9.6 to /home/testpoetry/.pyenv/versions/3.9.6

$ pyenv install 3.11.1
Downloading Python-3.11.1.tar.xz...
-> https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tar.xz
Installing Python-3.11.1...
Installed Python-3.11.1 to /home/testpoetry/.pyenv/versions/3.11.1

$ pyenv global 3.11.1
$ python --version
Python 3.11.1

python-poetry.orgより
poetryを導入します。

$ curl -sSL https://install.python-poetry.org | python3 -
Retrieving Poetry metadata

# Welcome to Poetry!

This will download and install the latest version of Poetry,
a dependency and package manager for Python.

It will add the `poetry` command to Poetry's bin directory, located at:

/home/testpoetry/.local/bin

You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.

Installing Poetry (1.3.1): Creating environment
Installing Poetry (1.3.1): Done

Poetry (1.3.1) is installed now. Great!

To get started you need Poetry's bin directory (/home/testpoetry/.local/bin) in your `PATH`
environment variable.

Add `export PATH="/home/testpoetry/.local/bin:$PATH"` to your shell configuration file.

Alternatively, you can call Poetry explicitly with `/home/testpoetry/.local/bin/poetry`.

You can test that everything is set up by executing:

`poetry --version`

.bashrcにPATHに’$HOME/.local/bin’を追記します。

export PYENV_ROOT="$HOME/.pyenv"
##export PATH="$PYENV_ROOT/bin:$PATH"
export PATH="$PYENV_ROOT/bin:$PATH:$HOME/.local/bin"
eval "$(pyenv init -)"

利用できるか確認します。

$ poetry --version
Poetry (version 1.3.1)

テスト用の環境を作成します。

$ mkdir testsend2
$ cd testsend2/
$ poetry init

This command will guide you through creating your pyproject.toml config.

Package name [testsend2]:
Version [0.1.0]:
Description []:
Author [None, n to skip]:  n
License []:
Compatible Python versions [^3.11]:

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): this will search for matches on PyPI
  - 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)

Package to add or search for (leave blank to skip):

Would you like to define your development dependencies interactively? (yes/no) [yes]
Package to add or search for (leave blank to skip):

Generated file

[tool.poetry]
name = "testsend2"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"


Do you confirm generation? (yes/no) [yes]
 
$ ls -l
合計 4
-rw-rw-r-- 1 testpoetry testpoetry 265  19 13:25 pyproject.toml

次にpoetryのvirtualenvをVScodeの認識させます。
(VScodeでDjangoのファイルを開いた時に波線で警告とならないように)

$ poetry --version
Poetry (version 1.3.1)

$ poetry config virtualenvs.in-project true --local

$ poetry env remove 3.xx.y
  先にpoetry shellを実行して
   poetry env use 3.xx.yを実行している場合のみ実施
   既存のvirtualenvを削除します。
 
$ poetry install
Creating virtualenv testsend2 in /home/testpoetry/testsend2/.venv
Updating dependencies
Resolving dependencies... (0.1s)

Writing lock file

$ ls -la
合計 24
drwxrwxr-x  3 testpoetry testpoetry 4096  19 13:53 .
drwxr-xr-x 16 testpoetry testpoetry 4096  19 13:51 ..
drwxrwxr-x  4 testpoetry testpoetry 4096  19 13:53 .venv
-rw-rw-r--  1 testpoetry testpoetry  239  19 13:53 poetry.lock
-rw-------  1 testpoetry testpoetry   32  19 13:52 poetry.toml
-rw-rw-r--  1 testpoetry testpoetry  262  19 13:51 pyproject.toml

仮想環境へ入って任意のモジュール(以下の例はDjango)を導入します。

$ source .venv/bin/activate
(testsend2-py3.11) testsend2$ poetry add django@3.2.7

Updating dependencies
Resolving dependencies... (0.5s)

Writing lock file

Package operations: 4 installs, 0 updates, 0 removals

  • Installing asgiref (3.6.0)
  • Installing pytz (2022.7)
  • Installing sqlparse (0.4.3)
  • Installing django (3.2.7)

$ django-admin startproject config .
$ ls
config  manage.py  poetry.lock  poetry.toml  pyproject.toml

無事導入出来ました。

途中で以下のエラーなる場合がありますが、メッセージに従い対応すると
復旧しました。
keyringrc.cfgの場所が問題となるようです。

$ poetry add django@3.2.7

Failed to create the collection: Prompt dismissed..
 
この場合は、keyring --disable を入力すると直るとGit上にありますので入力します。
ここで直る場合もあります。

$ keyring --disable
$ poetry add django@3.2.7

Keyring config exists only in the old location /home/testpoetry/.local/share/python_keyring/keyringrc.cfg and should be moved to /home/testpoetry/.config/python_keyring/keyringrc.cfg to work with this version of keyring.
 
ここで違うエラーとなりますがメッセージに従い対応策を実施します。
$ mkdir /home/testpoetry2/.config/python_keyring/
$ cd /home/testpoetry2/.local/share/python_keyring/
$ mv keyringrc.cfg /home/testpoetry2/.config/python_keyring/
 
$  poetry add django@3.2.7

Updating dependencies
Resolving dependencies... (0.3s)

Writing lock file

Package operations: 4 installs, 0 updates, 0 removals

  • Installing asgiref (3.6.0)
  • Installing pytz (2022.7)
  • Installing sqlparse (0.4.3)
  • Installing django (3.2.7)


では





この記事が気に入ったらサポートをしてみませんか?