Open7

Docker Python Poetry

Teru roomTeru room
  • sharl@macMini % mkdir -p ~/dev/python;cd ~/dev/python;pwd
/Users/sharl/dev/python
  • sharl@macMini python % docker run --rm -it python:3.11.4
実行結果
Unable to find image 'python:3.11.4' locally
3.11.4: Pulling from library/python
a014e5e7d08c: Pull complete 
715cea74ecbb: Pull complete 
003f1109a212: Pull complete 
a56ae3b61eb9: Pull complete 
c3668095a3a2: Pull complete 
c97c642cc269: Pull complete 
7d29ac33143c: Pull complete 
212179200d7f: Pull complete 
Digest: sha256:85b3d192dddbc96588b719e86991e472b390805a754681a38132de1977d8e429
Status: Downloaded newer image for python:3.11.4
Python 3.11.4 (main, Aug 16 2023, 07:34:21) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
  • >>> print("こんにちわ")
こんにちわ
  • >>> ctrl-d
Teru roomTeru room
  • sharl@macMini python % docker run --rm -it python:3.11.4 bash
  • root@a5c711009320:/# python --version
Python 3.11.4
  • root@a5c711009320:/# exit
exit
Teru roomTeru room

poetryのインストール

  • root@258e0ae13d1f:/# 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:

/root/.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.6.1): Done

Poetry (1.6.1) is installed now. Great!

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

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

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

You can test that everything is set up by executing:

`poetry --version`
  • root@258e0ae13d1f:/# ls -l $HOME/.local/bin
total 0
lrwxrwxrwx 1 root root 43 Oct  1 04:49 poetry -> /root/.local/share/pypoetry/venv/bin/poetry
Teru roomTeru room

poetoryの初期化

  • root@258e0ae13d1f:/# mkdir -p ~/app/python;cd ~/app/python
  • root@258e0ae13d1f:~/app/python# ~/.local/bin/poetry init
This command will guide you through creating your pyproject.toml config.
  • Package name []: sampleapp
  • Version [0.1.0]:
  • Description []: Sample App
  • Author [None, n to skip]: Teruroom
  • 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 = "sampleapp"
version = "0.1.0"
description = "Sample App"
authors = ["Teruroom"]
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]
Teru roomTeru room

pyproject.tomlファイルを確認

  • root@258e0ae13d1f:~/app/python# ls
pyproject.toml

venv仮想環境ができることを抑止

  • root@258e0ae13d1f:~/app/python# ~/.local/bin/poetry config virtualenvs.create false

通常のライブラリを追加

  • root@258e0ae13d1f:~/app/python# ~/.local/bin/poetry add boto3
実行結果
Skipping virtualenv creation, as specified in config file.
Using version ^1.28.57 for boto3

Updating dependencies
Resolving dependencies... Downloading https://files.pythonhosted.org/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf2Resolving dependencies... (1.2s)

Package operations: 7 installs, 0 updates, 0 removals

  • Installing six (1.16.0)
  • Installing jmespath (1.0.1)
  • Installing python-dateutil (2.8.2)
  • Installing urllib3 (1.26.16)
  • Installing botocore (1.31.57)
  • Installing s3transfer (0.7.0)
  • Installing boto3 (1.28.57)

Writing lock file

開発用のライブラリを追加

  • root@258e0ae13d1f:~/app/python# **~/.local/bin/poetry add ipython --group dev**
実行結果
Skipping virtualenv creation, as specified in config file.
Using version ^8.16.0 for ipython

Updating dependencies
Resolving dependencies... (3.0s)

Package operations: 17 installs, 0 updates, 0 removals

  • Installing asttokens (2.4.0)
  • Installing executing (2.0.0)
  • Installing parso (0.8.3)
  • Installing ptyprocess (0.7.0)
  • Installing pure-eval (0.2.2)
  • Installing traitlets (5.10.1)
  • Installing wcwidth (0.2.8)
  • Installing backcall (0.2.0)
  • Installing decorator (5.1.1)
  • Installing jedi (0.19.0)
  • Installing matplotlib-inline (0.1.6)
  • Installing pexpect (4.8.0)
  • Installing pickleshare (0.7.5)
  • Installing prompt-toolkit (3.0.39)
  • Installing pygments (2.16.1)
  • Installing stack-data (0.6.3)
  • Installing ipython (8.16.0)

Writing lock file
Teru roomTeru room

作成された設定ファイルを確認

  • root@258e0ae13d1f:~/app/python# ls -l
total 20
-rw-r--r-- 1 root root 15248 Oct  1 12:06 poetry.lock
-rw-r--r-- 1 root root   330 Oct  1 12:06 pyproject.toml
  • root@258e0ae13d1f:~/app/python# tail poetry.lock
実行結果
python-versions = "*"
files = [
    {file = "wcwidth-0.2.8-py2.py3-none-any.whl", hash = "sha256:77f719e01648ed600dfa5402c347481c0992263b81a027344f3e1ba25493a704"},
    {file = "wcwidth-0.2.8.tar.gz", hash = "sha256:8705c569999ffbb4f6a87c6d1b80f324bd6db952f5eb0b95bc07517f4c1813d4"},
]

[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "8f88b2a201b635e4ba96270d912d9e13efca679d9a0971b84bb9a4476a86c5b5"
  • root@258e0ae13d1f:~/app/python# cat pyproject.toml
実行結果
[tool.poetry]
name = "sampleapp"
version = "0.1.0"
description = "Sample App"
authors = ["Teruroom"]
readme = "README.md"

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


[tool.poetry.group.dev.dependencies]
ipython = "^8.16.0"

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

pyproject.tomlとpoetry.lockの情報をもとにライブラリーをインストール

  • root@258e0ae13d1f:~/app/python# cp pyproject.toml ../python2/;cp poetry.lock ../python2/; cd ../python2
  • root@258e0ae13d1f:~/app/python2# ~/.local/bin/poetry install
Skipping virtualenv creation, as specified in config file.
Installing dependencies from lock file

No dependencies to install or update
  • root@258e0ae13d1f:~/app/python2# ~/.local/bin/poetry update
Skipping virtualenv creation, as specified in config file.
Updating dependencies
Resolving dependencies... (0.7s)

No dependencies to install or update