Giter Site home page Giter Site logo

python_research_enviroment's Introduction

研究用pyhon環境

とりあえずすぐ始める方法

1.リポジトリをクローンします:

git clone <repository_url>

2.以下のコマンドを実行して、既存のgitを削除し、新しいgitを作成します:

rm -rf .git

3.仮想環境を作成します。:

poetry install

手を動かしたい場合

1. Pythonのインストール

Pyenvを使用して指定のバージョンのPythonをインストールします:

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
git clone https://github.com/pyenv/pyenv-update.git $(pyenv root)/plugins/pyenv-update
pyenv update
cd ~/.pyenv && git fetch && git pull
pyenv local <python-version>
pyenv global <python-version>

2. Poetryのインストール

curl -sSL https://install.python-poetry.org | python3 -
echo PATH="$HOME/.local/bin:$PATH" >> ~/.bashrc 
poetry config virtualenvs.in-project true 

3. ディレクトリの作成、Python環境の構築

mkdir python-demo
cd python-demo
pyenv install 3.11.0
pyenv local 3.11.0
python -m venv .venv
poetry init
poetry add <package>
poetry update
poetry run python sample.py

4. 静的解析、自動整形ツールのインストール

poetry add --group dev flake8 pyproject-flake8==6.0.4 flake8-isort flake8-bugbear flake8-builtins flake8-eradicate flake8-unused-arguments flake8-pytest-style pep8-naming mypy black isort ruff

pyproject.tomlに下記を追加します:

[tool.ruff]
target-version = "py38"
line-length = 100
select = [
  "E", # pycodestyle errors
  "W", # pycodestyle warnings
  "F", # pyflakes
  "B", # flake8-bugbear
  "I", # isort
]

ignore = [
  "E501", # line too long, handled by black
  "B008", # do not perform function calls in argument defaults
]

unfixable = [
  "F401", # module imported but unused
  "F841", # local variable is assigned to but never used
]
[tool.mypy]
# エラー時のメッセージを詳細表示
show_error_context = true
# エラー発生箇所の行数/列数を表示
show_column_numbers = true
# import 先のチェックを行わない (デフォルトだとサードパーティーライブラリまでチェックする)
ignore_missing_imports = true
# 関数定義の引数/戻り値に型アノテーション必須
disallow_untyped_defs = true
# デフォルト引数に None を取る場合型アノテーションに Optional 必須
no_implicit_optional = true
# 戻り値が Any 型ではない関数の戻り値の型アノテーションが Any のとき警告
warn_return_any = true
# mypy エラーに該当しない箇所に `# type: ignore` コメントが付与されていたら警告
# ※ `# type: ignore` が付与されている箇所は mypy のエラーを無視出来る
warn_unused_ignores = true
# 冗長なキャストに警告
warn_redundant_casts = true

[tool.black]
line-length = 79

[tool.isort]
profile = "black"
line_length = 79
# 各ライブラリ群の説明を追記する
import_heading_stdlib      = "Standard Library"
import_heading_thirdparty  = "Third Party Library"
import_heading_firstparty  = "First Party Library"
import_heading_localfolder = "Local Library"
# from third_party import lib1, lib2...のような記述時の改行方法の設定(https://pycqa.github.io/isort/docs/configuration/multi_line_output_modes.html)
multi_line_output = 3
# 最後の要素の末尾に","を付けるようにする設定
include_trailing_comma = true

[tool.flake8]
max-line-length = 79
# E203: ":"の前の空白を入れないルール
# W503: 演算子の前に改行しないようにするルール
extend-ignore = ["E203", "W503"]
exclude = [".venv", ".git", "__pycache__",]
max-complexity = 10

VS Codeのsettings.jsonに以下を追加します:

{
    "[python]": {
        "editor.tabSize": 4,
        "editor.formatOnSave": true
    },
    "python.analysis.extraPaths": ["./src"],
    "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "${workspaceFolder}/.venv/bin/black",
    "python.sortImports.path": "${workspaceFolder}/.venv/bin/isort",
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--ignore=E203, W503",
        "--max-complexity=10"
    ],
    "python.linting.flake8Path": "${workspaceFolder}/.venv/bin/flake8",
    "python.linting.mypyEnabled": true,
    "python.linting.mypyPath": "${workspaceFolder}/.venv/bin/mypy",
    "autoDocstring.docstringFormat": "numpy"
}

5. Dockerfileとdocker-compose

これらはこちらのgithubからコピペ推奨です。

pytorchを使用する場合,こちらのサイト参考にしてください。

6. ディレクトリの構成

ディレクトリ構成は自由ですが、こちらを参照してください。

7. ハードコーディングをさせるために

config.pyのdataclassを使用してください。

dataclassの参考資料

改善

もっと上手い人がいた場合、改善をお願いします。

Pull requestの出し方

こちらを参照してください。

開発環境

  • 計算機(実際にプログラムを動かすマシン)のOS:Ubuntu
  • コードエディタ:VS Code

SSH接続

Github SSH接続

参考文献

やること

  • SSH接続
  • Python環境構築[Pyenv+Poetry]
  • 静的解析&自動整形[flake8+black+isort+mypy]
  • Makefileの作成
  • dockerの構成
  • ディレクトリ構成
  • github actionの追加
  • Pythonデバッグ (参考)
  • 実験設定の管理[ハードコーディングを避ける]
  • [WIP]適切なログの出力 printからの脱却ドキュメント

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.