Python demonstration, exercise, practice, study, example codes.
require : Python 3.8
using:
unittest discover: 3.2- pip as default : 3.6
dataclasses: 3.7,functools.singledispatchmethod: 3.8
setup venv and install pip requirements:
## create venv
cd (repository root directory)
python -m venv .venv
## activate venv
(linux-bash)
source .venv/bin/activate
(win-cmd)
.venv\Scripts\activate.bat
(other-shell, platform, see https://docs.python.org/ja/3/library/venv.html )
## update pip in venv
python -m pip install --upgrade pip
## install pip requirements
python -m pip install -r requirements.txt
run from command line:
cd (repository root directory)
(activate venv)
## run unittest:
python -m unittest discover
## run flake8:
flake8 snacks tests
## run mypy:
mypy snacks tests
## run black (dry-run)
black --diff snacks tests
## run black (write-back to files)
black snacks tests
before opening vscode, you MUST have done creating venv and pip install -r requirements
after opening vscode (first-time only):
- select "Python: Select Interpreter" command from the Command Palette (
Ctrl+Shift+P) - choose "Python 3.x.y ... .venv" (.venv python)
- NOTE: vscode automatically generate
.vscode/settings.jsonat this time.
- NOTE: vscode automatically generate
- add below settings to
.vscode/settings.json
{
"python.venvPath": ".venv",
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": true,
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"python.linting.lintOnSave": true,
"python.pythonPath": "(path inserted by vscode)"
}
- Why are you put
.vscode/settings.jsonto git repository ?- Because venv's
python.pythonPathis different between some platoforms. for Win,Scripts\\python.exe<> but linux/macos,bin/python. - I gave up putting these cross-platform behabiours to one-single-common
settings.jsonX(
- Because venv's
refs:
Using Python Environments in Visual Studio CodeVS CodeでWorkspace毎に使用するPython実行環境を切り替える | LogixSquare
venv --- 仮想環境の作成 � Python 3.8.2 ドキュメント12. 仮想環境とパッケージ � Python 3.8.2 ドキュメントPython モジュールのインストール � Python 3.8.2 ドキュメントvenv: Python 仮想環境管理 - Qiita
pip コマンドのヘルプ: python -m pip help
pip install したら pip freeze 結果を requirements.txt に保存する。そうすれば pip install -r requirements.txt で同じパッケージをインストールできる:
python -m pip freeze -l > requirements.txt- https://pip.pypa.io/en/stable/reference/
Python, pip list / freezeでインストール済みパッケージ一覧を確認 | note.nkmk.mePython, pipでrequirements.txtを使ってパッケージ一括インストール | note.nkmk.meよく使うpipコマンド - Qiita
-
flake8
-
mypy
-
black (formatter)
-
Python開発を円滑に進めるためのツール設定 Part.1 - ログミーTech -
Python開発を円滑に進めるためのツール設定 Part.2 - ログミーTech -
Pythonでの開発を効率的に進めるためのツール設定 -
Python 3.7とVisual Studio Codeで型チェックが捗る内作Pythonアプリケーション開発環境の構築 - Qiita -
超簡単VSCodeでPythonソースコード自動チェック・整形(venv・flake8・mypy・black利用)|10mohi6|note -
Visual Studio Code を使ったPython環境の構築 - Qiita -
もうPythonの細かい書き方で議論しない。blackで自動フォーマットしよう - Make組ブログ
- https://docs.python.org/ja/3/library/unittest.html
- https://pymotw.com/3/unittest/
- Python 3 標準の unittest でテストを書く際のディレクトリ構成 - Qiita
- Pythonのunittestでハマったところと、もっと早くに知りたかったこと - Qiita
- あるディレクトリ以下の unittest を全部実行させる - Qiita
- https://coding-exercises.udemy.com/python_3.html
- https://realpython.com/python-testing/#executing-your-first-test
- https://realpython.com/python-mock-library/
- https://pep8-ja.readthedocs.io/ja/latest/
- [Python入門]Pythonコーディングスタイルガイド:Python入門 - @IT