gamiyama3110 / pythondbox

python触る
0 stars 0 forks source link

簡易開発環境の構築 #1

Open gamiyama3110 opened 4 years ago

gamiyama3110 commented 4 years ago

概要

仮想環境を使った開発環境を構築。 ↓ venvはもう古いのか https://www.m3tech.blog/entry/python-packaging

対応

複数の人が触ることを考慮した構成にする。 ここの導入はテンプレート化できたらいいな。

制約・特記

flake8 静的コード解析ツールのラッパ

mypy 静的型チェックツール 正しい型が使われているかチェックできる。

black コードフォーマッタ。 pep8に加えた独自のコードスタイルで整形する。 blackは設定できる項目が少なく、ルールが限定されていて悩みが減る。

これ以外にも、autopep8やyapfもある。

その他

gamiyama3110 commented 4 years ago
$ docker run --name="work-python" -it python3
[root@4b3b75888f30 /]# python3 -V
Python 3.6.8
[root@4b3b75888f30 /]# pip3 show pip
Name: pip
Version: 9.0.3
Summary: The PyPA recommended tool for installing Python packages.
Home-page: https://pip.pypa.io/
Author: The pip developers
Author-email: python-virtualenv@groups.google.com
License: MIT
Location: /usr/lib/python3.6/site-packages
Requires:

dockerより仮想環境のがいいか。 ↓ 仮想環境も古い

gamiyama3110 commented 4 years ago

https://qiita.com/y-tsutsu/items/54c10e0b2c6b565c887a pipenvがイケてそう

pipenvのインストール

pip install pipenv

初期化

# 指定バージョンのpythonでディレクトリ(プロジェクト)を初期化
pipenv --python 3.8

# ライブラリをインストール
pipenv install hogehoge

# 開発環境用のライブラリをインストール
pipenv install --dev flake8 mypy black tox

black, toxを単体でインストールした時につまづいたこと。

Locking Filed!

new_channel_notification[master #]$ pipenv install --dev tox
Installing tox…
Adding tox to Pipfile's [dev-packages]…
✔ Installation Succeeded 
Pipfile.lock (2f9ca5) out of date, updating to (1e763c)…
Locking [dev-packages] dependencies…
Building requirements...
Resolving dependencies...
✘ Locking Failed! 
[ResolutionFailure]:   File "/usr/local/lib/python3.8/site-packages/pipenv/resolver.py", line 785, in _main
[ResolutionFailure]:       resolve_packages(pre, clear, verbose, system, write, requirements_dir, packages)
[ResolutionFailure]:   File "/usr/local/lib/python3.8/site-packages/pipenv/resolver.py", line 746, in resolve_packages
[ResolutionFailure]:       results, resolver = resolve(
[ResolutionFailure]:   File "/usr/local/lib/python3.8/site-packages/pipenv/resolver.py", line 728, in resolve
[ResolutionFailure]:       return resolve_deps(
[ResolutionFailure]:   File "/usr/local/lib/python3.8/site-packages/pipenv/utils.py", line 1378, in resolve_deps
[ResolutionFailure]:       results, hashes, markers_lookup, resolver, skipped = actually_resolve_deps(
[ResolutionFailure]:   File "/usr/local/lib/python3.8/site-packages/pipenv/utils.py", line 1093, in actually_resolve_deps
[ResolutionFailure]:       resolver.resolve()
[ResolutionFailure]:   File "/usr/local/lib/python3.8/site-packages/pipenv/utils.py", line 818, in resolve
[ResolutionFailure]:       raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: Could not find a version that matches black (from -r /var/folders/4x/k3zjtz2n07v0b7m7rn86cdjc0000gq/T/pipenv36gobi4srequirements/pipenv-9yfdbkhw-constraints.txt (line 3))
Skipped pre-versions: 18.3a0, 18.3a0, 18.3a1, 18.3a1, 18.3a2, 18.3a2, 18.3a3, 18.3a3, 18.3a4, 18.3a4, 18.4a0, 18.4a0, 18.4a1, 18.4a1, 18.4a2, 18.4a2, 18.4a3, 18.4a3, 18.4a4, 18.4a4, 18.5b0, 18.5b0, 18.5b1, 18.5b1, 18.6b0, 18.6b0, 18.6b1, 18.6b1, 18.6b2, 18.6b2, 18.6b3, 18.6b3, 18.6b4, 18.6b4, 18.9b0, 18.9b0, 19.3b0, 19.3b0, 19.10b0, 19.10b0
There are incompatible versions in the resolved dependencies:

$ pipenv clean
$ pipenv lock --clear
$ pipenv lock --pre

で解消できた

仮想環境

pipenv shell

既存環境をリビルド

# 本番
pipenv install
# dev
pipenv install --dev

開発用コマンドメモ

flake8 --show-source .
mypy src/.
black --check src/
black --diff src/
tox
tox -e flake8
gamiyama3110 commented 4 years ago

setup.cfg


[mypy]
ignore_missing_imports = true
check_untyped_defs = true
incremental = true

[flake8] max-line-length = 119 max-complexity = 10 exclude = migrations, urls.py, manage.py, wsgi.py import-order-style = google


> pyproject.toml

[tool.black] line-length = 119 target-version = ['py38'] include = '.pyi?$' exclude = ''' ( .git | .mypy_cache | .pytest_cache | .tox | .venv | migrations ) '''


> tox.ini

[tox] envlist = black, flake8, mypy

application開発はtrue, lib開発はfalse

skipsdist = true

[testenv:flake8] deps = flake8 commands = flake8 --show-source src/.

[testenv:mypy] deps = mypy commands = mypy src/.

[testenv:black] deps = black commands = black src/.

gamiyama3110 commented 4 years ago

https://www.magata.net/memo/index.php?pytest%C6%FE%CC%E7#y2046859 testコード