70
98

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python開発環境構築手順(VSCode, Docker, Poetry, isort, black, flake8, pytest)

Last updated at Posted at 2022-11-19

はじめに

  • Python開発環境を整備し直したときのメモです
  • コードはこちらに配置しています

どういう開発環境を作るか

  • VSCode・Docker上で実行する(ローカルでの実行を想定)
  • パッケージ管理はPoetryを利用する
  • linter, formatterはisort, black, flake8を利用する
    • コマンドだけでなくVSCodeからも利用する
  • テストはpytestを利用する
  • Jupyter Notebookも利用可能にしておく

セットアップ手順

事前準備【ローカル】

  • VSCode・Dockerのインストール
  • VSCode拡張機能Remote Developmentのインストール

設定ファイルの配置

  • (参考)フォルダ構成
.
├── .devcontainer
│   └── devcontainer.json
├── .vscode
│   ├── launch.json
│   └── settings.json
├── Dockerfile
├── pyproject.toml
├── src
│   └── sample.py
└── tests
    └── test_sample.py
.devcontainer/devcontainer.json
{
    "name": "Python39docker",
    "dockerFile": "../Dockerfile",
    "extensions": [
        "ms-python.python",
        "ms-python.vscode-pylance",
        "njpwerner.autodocstring",
        "mosapride.zenkaku",
        "streetsidesoftware.code-spell-checker"
    ],
    "forwardPorts": [8888]
}
.vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python39docker",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "internalConsole"
        }
    ]
}
.vscode/settings.json
{
    //isort
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    },
    //black
    "editor.formatOnSave": true,
    "python.formatting.provider": "black",
    //flake8
    "python.linting.lintOnSave": true,
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    //autoDocstring
    "autoDocstring.docstringFormat": "google"
}
Dockerfile
FROM python:3.9

# install python package
COPY pyproject.toml ./
RUN pip install poetry
RUN poetry config virtualenvs.create false \
    && poetry install --no-root

# mount dir
RUN mkdir -p /opt/mnt
WORKDIR /opt/mnt

# expose port
EXPOSE 8888
  • pyproject.toml
    • packages: sys.pathに自動的に含まれるようになる(tests配下からのパスなどで利用する)
    • (参考)その設定、pyproject.tomlに全部書けます
    • まず手動で poetry add pandas とかで試してからファイルに追記する
pyproject.toml
[tool.poetry]
name = "sample"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
packages = [
    { include = "src" },
]

[tool.poetry.dependencies]
python = "^3.9"
pandas = "^1.5.1"
isort = "^5.10.1"
black = "^22.10.0"
pyproject-flake8 = "^5.0.4.post1"
pytest = "^7.2.0"
jupyterlab = "^3.5.0"

[tool.isort]
profile = "black"
line_length = 120

[tool.black]
line-length = 120

[tool.flake8]
max-line-length = 120
ignore = "E203,"

[tool.pytest.ini_options]
testpaths = ["tests",]
  • src/sample.py
    • サンプルコード
    • (参考)pytest
src/sample.py
import sys


def is_prime(n: int) -> bool:
    if n <= 1:
        return False

    if n == 2:
        return True

    if n % 2 == 0:
        return False

    i = 3

    while i * i <= n:
        if n % i == 0:
            return False

        i += 2

    return True

if __name__ == '__main__':
    args = sys.argv
    n = int(args[1])
    print(is_prime(n))
  • tests/test_sample.py
    • サンプルのテストコード
    • (参考)pytest
tests/test_sample.py
from src.sample import is_prime


def test_is_prime():
    assert not is_prime(1)
    assert is_prime(2)
    assert is_prime(3)
    assert not is_prime(4)
    assert is_prime(5)
    assert not is_prime(6)
    assert is_prime(7)
    assert not is_prime(8)
    assert not is_prime(9)
    assert not is_prime(10)

コンテナ起動・接続

  • コンテナ起動
$ docker build -t python39docker .
$ docker run -itd -v $(pwd):/opt/mnt -p 18888:8888 python39docker
  • コンテナにVSCodeからの接続
    • VSCode左下のリモートウィンドウ → Attach to Running Container

→ コンテナ側のVSCodeが起動するので、開発する

その他

  • linter, formatter実行
$ poetry run isort src tests
$ poetry run black src tests
$ poetry run flake8 src tests
  • テスト実行
# packageを読み込んでパス設定
$ poetry install
# テスト実行
$ poetry run pytest src tests
  • Jupyter Notebook起動
$ jupyter notebook --allow-root --port=8888 --ip=0.0.0.0 &
# jupyter lab --allow-root --port=8888 --ip=0.0.0.0 &
70
98
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
70
98

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?