{"meta":{"title":"Pythonの構築とテスト","intro":"継続的インテグレーション (CI) ワークフローを作成して、Python プロジェクトをビルドしてテストする方法について説明します。","product":"GitHub Actions","breadcrumbs":[{"href":"/ja/actions","title":"GitHub Actions"},{"href":"/ja/actions/tutorials","title":"チュートリアル"},{"href":"/ja/actions/tutorials/build-and-test-code","title":"コードのビルドとテスト"},{"href":"/ja/actions/tutorials/build-and-test-code/python","title":"Python"}],"documentType":"article"},"body":"# Pythonの構築とテスト\n\n継続的インテグレーション (CI) ワークフローを作成して、Python プロジェクトをビルドしてテストする方法について説明します。\n\n## はじめに\n\nこのガイドでは、Python パッケージをビルド、テスト、発行する方法について説明します。\n\nGitHub によってホストされるランナーは、Python と PyPy を保持しているプリインストール済みソフトウェアと共にツール キャッシュを確保しています。 自分では何もインストールする必要がありません！ 最新のソフトウェアと、Python および PyPy のプリインストール済みのバージョンの完全な一覧については、[GitHub ホステッド ランナー](/ja/actions/using-github-hosted-runners/about-github-hosted-runners#supported-software) を参照してください。\n\n## 前提条件\n\nYAMLとGitHub Actionsの構文に馴染んでいる必要があります。 詳しくは、「[ワークフローの書き込み](/ja/actions/learn-github-actions)」をご覧ください。\n\nPythonと pip の基本的な理解をお勧めします。 詳細については、以下を参照してください:\n\n* [Pythonを始める](https://www.python.org/about/gettingstarted/)\n* [pip パッケージ マネージャー](https://pypi.org/project/pip/)\n\n## Python ワークフロー テンプレートの使用\n\nすぐに開始するには、リポジトリの `.github/workflows` ディレクトリにワークフロー テンプレートを追加します。\n\nGitHub には、リポジトリに少なくとも 1 つの `.py` ファイルが既に含まれている場合に機能するPythonのワークフロー テンプレートが用意されています。 このガイドの以降のセクションでは、このワークフロー テンプレートをカスタマイズする方法の例を示します。\n\n1. GitHub で、リポジトリのメイン ページに移動します。\n\n2. リポジトリ名の下にある **\\[<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-play\" aria-label=\"play\" role=\"img\"><path d=\"M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Zm4.879-2.773 4.264 2.559a.25.25 0 0 1 0 .428l-4.264 2.559A.25.25 0 0 1 6 10.559V5.442a.25.25 0 0 1 .379-.215Z\"></path></svg> Actions]** をクリックします。\n\n   ![\"github/docs\" リポジトリのタブのスクリーンショット。 \\[アクション\\] タブがオレンジ色の枠線で強調表示されています。](/assets/images/help/repository/actions-tab-global-nav-update.png)\n\n3. ワークフローが既にリポジトリ内にある場合は、 **\\[新しいワークフロー]** をクリックします。\n\n4. \\[ワークフローの選択] ページには、推奨されるワークフロー テンプレートの選択が表示されます。 \"Python アプリケーション\" を検索します。\n\n5. \\[Python アプリケーション] ワークフローで、**Configure** をクリックします。\n\n6. 必要に応じてワークフローを編集します。 たとえば、Pythonバージョンを変更します。\n\n7. ```\n          **[Commit changes]** をクリックします。\n   ```\n\n`python-app.yml` ワークフロー ファイルが使用中リポジトリの `.github/workflows` ディレクトリに追加されます。\n\n## Python バージョンの指定\n\nGitHub ホスト ランナー上で Python または PyPy のプレインストールされたバージョンを使うには、`setup-python` アクションを使用します。 このアクションでは、各ランナーのツール キャッシュから特定のバージョンのPythonまたは PyPy を検索し、必要なバイナリを `PATH` に追加します。このバイナリはジョブの残りの部分で保持されます。 特定のバージョンのPythonがツール キャッシュにプレインストールされていない場合、`setup-python` アクションは、[`python-versions`](https://github.com/actions/python-versions) リポジトリから適切なバージョンをダウンロードして設定します。\n\n```\n          `setup-python` アクションを使用することは、GitHub Actions でPythonを使用するお勧めの方法です。これは、異なるランナーやPythonのバージョン間で一貫した動作を確保できるためです。 セルフホステッド ランナーを使用している場合は、Pythonをインストールし、`PATH` に追加する必要があります。 詳細については、「[`setup-python`アクション](https://github.com/marketplace/actions/setup-python)」を参照してください。\n```\n\n以下の表は、各GitHubホストランナー内でのツールキャッシュの場所です。\n\n<div class=\"ghd-tool rowheaders\">\n\n|                      | Ubuntu                          | Mac                                      | Windows                               |\n| -------------------- | ------------------------------- | ---------------------------------------- | ------------------------------------- |\n| **ツール キャッシュ ディレクトリ** | `/opt/hostedtoolcache/*`        | `/Users/runner/hostedtoolcache/*`        | `C:\\hostedtoolcache\\windows\\*`        |\n| **Python ツール キャッシュ** | `/opt/hostedtoolcache/Python/*` | `/Users/runner/hostedtoolcache/Python/*` | `C:\\hostedtoolcache\\windows\\Python\\*` |\n| **PyPy ツール キャッシュ**   | `/opt/hostedtoolcache/PyPy/*`   | `/Users/runner/hostedtoolcache/PyPy/*`   | `C:\\hostedtoolcache\\windows\\PyPy\\*`   |\n\n</div>\n\nセルフホスト ランナーを使用している場合は、`setup-python` アクションを使って依存関係を管理するようにランナーを構成できます。 詳細については、[自己ホストランナーでの setup-python の使用](https://github.com/actions/setup-python#using-setup-python-with-a-self-hosted-runner)に関する`setup-python` README を参照してください。\n\nGitHubは、セマンティックバージョン構文をサポートしています。 詳細については、[セマンティック バージョニングの使用](https://docs.npmjs.com/about-semantic-versioning#using-semantic-versioning-to-specify-update-types-your-package-can-accept)と[セマンティック バージョニングの仕様](https://semver.org/)に関するページを参照してください。\n\n### 複数のPython バージョンの使用\n\n次の例では、ジョブのマトリックスを使用して、複数のPython バージョンを設定します。 詳しくは、「[ワークフローでのジョブのバリエーションの実行](/ja/actions/using-jobs/using-a-matrix-for-your-jobs)」をご覧ください。\n\n```yaml copy\nname: Python package\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        python-version: [\"pypy3.10\", \"3.9\", \"3.10\", \"3.11\", \"3.12\", \"3.13\"]\n\n    steps:\n      - uses: actions/checkout@v5\n      - name: Set up Python ${{ matrix.python-version }}\n        uses: actions/setup-python@v5\n        with:\n          python-version: ${{ matrix.python-version }}\n      # You can test your matrix by printing the current Python version\n      - name: Display Python version\n        run: python -c \"import sys; print(sys.version)\"\n```\n\n### 特定のPython バージョンの使用\n\n特定のバージョンのPythonを構成できます。 たとえば、3.12 などです。 あるいは、最新のマイナーリリースを取得するためにセマンティックバージョン構文を使うこともできます。 この例では、Python 3 の最新のマイナー リリースを使用します。\n\n```yaml copy\nname: Python package\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n\n    steps:\n      - uses: actions/checkout@v5\n      - name: Set up Python\n        # This is the version of the action for setting up Python, not the Python version.\n        uses: actions/setup-python@v5\n        with:\n          # Semantic version range syntax or exact version of a Python version\n          python-version: '3.x'\n          # Optional - x64 or x86 architecture, defaults to x64\n          architecture: 'x64'\n      # You can test your matrix by printing the current Python version\n      - name: Display Python version\n        run: python -c \"import sys; print(sys.version)\"\n```\n\n### バージョンの除外\n\n使用できないPythonのバージョンを指定した場合、`setup-python` は次のようなエラーで失敗`##[error]Version 3.7 with arch x64 not found`。 このエラーメッセージには、利用できるバージョンが含まれます。\n\n実行しないPythonの構成がある場合は、ワークフローで `exclude` キーワードを使用することもできます。 詳しくは、「[GitHub Actions　のワークフロー構文](/ja/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategy)」をご覧ください。\n\n```yaml copy\nname: Python package\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ${{ matrix.os }}\n    strategy:\n      matrix:\n        os: [ubuntu-latest, macos-latest, windows-latest]\n        python-version: [\"3.9\", \"3.11\", \"3.13\", \"pypy3.10\"]\n        exclude:\n          - os: macos-latest\n            python-version: \"3.11\"\n          - os: windows-latest\n            python-version: \"3.11\"\n```\n\n### 既定のPython バージョンの使用\n\n```\n          `setup-python` を使用して、ワークフローで使用されるPythonのバージョンを構成することをお勧めします。これは、依存関係を明示的にするのに役立ちます。 \n          `setup-python` を使用しない場合、`PATH` を呼び出すときに、`python` で設定された既定のバージョンのPythonが任意のシェルで使用されます。 Pythonの既定のバージョンは、GitHubホストランナーによって異なります。これにより、予期しない変更が発生したり、予想よりも古いバージョンが使用されたりする可能性があります。\n```\n\n| GitHubによってホストされているランナー | 説明                                                                                                                                                                                 |\n| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| Ubuntu                 | Ubuntu ランナーには、`/usr/bin/python` および `/usr/bin/python3` の下に複数のバージョンのシステム Pythonがインストールされています。 Ubuntu にパッケージ化されたPythonバージョンは、GitHub がツール キャッシュにインストールするバージョンに加えてあります。                |\n| Windows                | ツール キャッシュ内のPythonのバージョンを除き、Windowsには同等のバージョンのシステム Pythonが付属していません。 他のランナーとの一貫した動作を維持し、`setup-python` アクションなしでPythonをすぐに使用できるようにするために、GitHub はツール キャッシュから `PATH` にいくつかのバージョンを追加します。 |\n| macOS                  | macOS ランナーには、ツール キャッシュの一部であるバージョンに加えて、複数のバージョンのシステム Pythonがインストールされています。 システム Pythonバージョンは、`/usr/local/Cellar/python/*` ディレクトリにあります。                                               |\n\n## 依存関係のインストール\n\nGitHubホストランナーには、pipパッケージマネージャーがインストールされています。 コードのビルドとテストに先立って、pipを使ってパッケージレジストリのPyPIから依存関係をインストールできます。 たとえば、以下の YAML では、`pip` パッケージ インストーラーと、`setuptools` および `wheel` パッケージがインストールまたはアップグレードされます。\n\nワークフローの速度を上げるために、依存関係をキャッシュすることもできます。 詳しくは、「[依存関係キャッシュのリファレンス](/ja/actions/using-workflows/caching-dependencies-to-speed-up-workflows)」をご覧ください。\n\n```yaml copy\nsteps:\n- uses: actions/checkout@v5\n- name: Set up Python\n  uses: actions/setup-python@v5\n  with:\n    python-version: '3.x'\n- name: Install dependencies\n  run: python -m pip install --upgrade pip setuptools wheel\n```\n\n### 要件ファイル\n\n```\n          `pip` の更新後、一般的な次の手順は、`requirements.txt` から依存関係をインストールすることです。 詳しくは、「[pip](https://pip.pypa.io/en/stable/cli/pip_install/#example-requirements-file)」をご覧ください。\n```\n\n```yaml copy\nsteps:\n- uses: actions/checkout@v5\n- name: Set up Python\n  uses: actions/setup-python@v5\n  with:\n    python-version: '3.x'\n- name: Install dependencies\n  run: |\n    python -m pip install --upgrade pip\n    pip install -r requirements.txt\n```\n\n### 依存関係のキャッシング\n\n```\n          [\n          `setup-python`アクション](https://github.com/actions/setup-python)を使用して依存関係をキャッシュおよび復元できます。\n```\n\n次の例では pip の依存関係をキャッシュします。\n\n```yaml copy\nsteps:\n- uses: actions/checkout@v5\n- uses: actions/setup-python@v5\n  with:\n    python-version: '3.12'\n    cache: 'pip'\n- run: pip install -r requirements.txt\n- run: pip test\n```\n\n既定で、`setup-python` アクションでは、リポジトリ全体で依存関係ファイル (pipは `requirements.txt`、pipenvは `Pipfile.lock`、poetryは `poetry.lock`) を検索します。 詳細については、`setup-python` の README の「[パッケージの依存関係のキャッシュ](https://github.com/actions/setup-python#caching-packages-dependencies)」をご覧ください。\n\nカスタム要件がある場合、またはキャッシュに対してより細かい制御が必要な場合は、[`cache` アクション](https://github.com/marketplace/actions/cache)を使用できます。 ランナーのオペレーティングシステムによって、pipは依存関係を様々な場所にキャッシュします。 キャッシュする必要があるパスは、使用するオペレーティング システムによって、上記の Ubuntu の例とは異なる場合があります。 詳細については、[](https://github.com/actions/cache/blob/main/examples.md#python---pip) アクション リポジトリの`cache`を参照してください。\n\n## コードのテスト\n\nローカルで使うのと同じコマンドを、コードのビルドとテストに使えます。\n\n### pytest及びpytest-covでのテスト\n\nこの例では、`pytest` および `pytest-cov` をインストールまたはアップグレードします。 そしてテストが実行され、JUnit形式で出力が行われ、一方でコードカバレッジの結果がCoberturaに出力されます。 詳細については、[JUnit](https://junit.org/junit5/) と [Cobertura](https://cobertura.github.io/cobertura/) に関するページを参照してください。\n\n```yaml copy\nsteps:\n- uses: actions/checkout@v5\n- name: Set up Python\n  uses: actions/setup-python@v5\n  with:\n    python-version: '3.x'\n- name: Install dependencies\n  run: |\n    python -m pip install --upgrade pip\n    pip install -r requirements.txt\n- name: Test with pytest\n  run: |\n    pip install pytest pytest-cov\n    pytest tests.py --doctest-modules --junitxml=junit/test-results.xml --cov=com --cov-report=xml --cov-report=html\n```\n\n### Ruff を使用してコードのリントや書式設定を行う\n\n以下の例では、`ruff` をインストールまたはアップグレードし、それを使ってすべてのファイルを lint します。 詳しくは、「[Ruff](https://docs.astral.sh/ruff)」を参照してください。\n\n```yaml copy\nsteps:\n- uses: actions/checkout@v5\n- name: Set up Python\n  uses: actions/setup-python@v5\n  with:\n    python-version: '3.x'\n- name: Install the code linting and formatting tool Ruff\n  run: pipx install ruff\n- name: Lint code with Ruff\n  run: ruff check --output-format=github --target-version=py39\n- name: Check code formatting with Ruff\n  run: ruff format --diff --target-version=py39\n  continue-on-error: true\n```\n\n書式設定ステップが `continue-on-error: true` に設定されています。 これにより、書式設定ステップが成功しなかった場合にワークフローが失敗しなくなります。 すべての書式設定エラーに対処したら、ワークフローで新しい問題を見つけられるようにこのオプションを削除できます。\n\n### toxでのテストの実行\n\nGitHub Actionsでは、toxでテストを実行し、その処理を複数のジョブに分散できます。 特定のバージョンを指定するのではなく、`-e py` オプションを使用して tox を呼び出して、`PATH` のPythonのバージョンを選択する必要があります。 詳細については、[tox](https://tox.readthedocs.io/en/latest/) に関するページを参照してください。\n\n```yaml copy\nname: Python package\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        python: [\"3.9\", \"3.11\", \"3.13\"]\n\n    steps:\n      - uses: actions/checkout@v5\n      - name: Setup Python\n        uses: actions/setup-python@v5\n        with:\n          python-version: ${{ matrix.python }}\n      - name: Install tox and any other packages\n        run: pip install tox\n      - name: Run tox\n        # Run tox using the version of Python in `PATH`\n        run: tox -e py\n```\n\n## 成果物としてのワークフローのデータのパッケージ化\n\nワークフローの完了後に、成果物をアップロードして見ることができます。 たとえば、ログファイル、コアダンプ、テスト結果、スクリーンショットを保存する必要があるかもしれません。 詳しくは、「[ワークフロー成果物を使ったデータの格納と共有](/ja/actions/using-workflows/storing-workflow-data-as-artifacts)」をご覧ください。\n\n以下の例は、`upload-artifact` アクションを使って `pytest` の実行によるテスト結果をアーカイブする方法を示しています。 詳細については、「[`upload-artifact`アクション](https://github.com/actions/upload-artifact)」を参照してください。\n\n```yaml copy\nname: Python package\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        python-version: [\"3.9\", \"3.10\", \"3.11\", \"3.12\", \"3.13\"]\n\n    steps:\n      - uses: actions/checkout@v5\n      - name: Setup Python # Set Python version\n        uses: actions/setup-python@v5\n        with:\n          python-version: ${{ matrix.python-version }}\n      # Install pip and pytest\n      - name: Install dependencies\n        run: |\n          python -m pip install --upgrade pip\n          pip install pytest\n      - name: Test with pytest\n        run: pytest tests.py --doctest-modules --junitxml=junit/test-results-${{ matrix.python-version }}.xml\n      - name: Upload pytest test results\n        uses: actions/upload-artifact@v4\n        with:\n          name: pytest-results-${{ matrix.python-version }}\n          path: junit/test-results-${{ matrix.python-version }}.xml\n        # Use always() to always run this step to publish test results when there are test failures\n        if: ${{ always() }}\n```\n\n## PyPI への発行\n\nCI テストに合格したら、Python パッケージを PyPI に発行するようにワークフローを構成できます。 このセクションでは、GitHub Actions を使用して、リリースを公開するたびにパッケージを PyPI にアップロードする方法について説明します。 詳しくは、「[リポジトリのリリースを管理する](/ja/repositories/releasing-projects-on-github/managing-releases-in-a-repository)」をご覧ください。\n\n次のワークフロー例では、[信頼された出版を設定する](https://docs.pypi.org/trusted-publishers/) を使用して PyPI で認証を行います。これには、手動で構成された API トークンは不要です。\n\n```yaml copy\n# このワークフローはGitHubによって認定されていないアクションを使用します。\n# それらはサードパーティによって提供され、\n# 別個の利用規約、プライバシーポリシー、\n# ドキュメントを参照してください。\n\n# GitHub では、コミット SHA にアクションをピン留めすることが推奨されます。\n# 新しいバージョンを取得するには、SHA を更新する必要があります。\n# タグまたはブランチを参照することもできますが、アクションは警告なしに変更される可能性があります。\n\nname: Upload Python Package\n\non:\n  release:\n    types: [published]\n\npermissions:\n  contents: read\n\njobs:\n  release-build:\n    runs-on: ubuntu-latest\n\n    steps:\n      - uses: actions/checkout@v5\n\n      - uses: actions/setup-python@v5\n        with:\n          python-version: \"3.x\"\n\n      - name: Build release distributions\n        run: |\n          # NOTE: put your own distribution build steps here.\n          python -m pip install build\n          python -m build\n\n      - name: Upload distributions\n        uses: actions/upload-artifact@v4\n        with:\n          name: release-dists\n          path: dist/\n\n  pypi-publish:\n    runs-on: ubuntu-latest\n\n    needs:\n      - release-build\n\n    permissions:\n      # IMPORTANT: this permission is mandatory for trusted publishing\n      id-token: write\n\n    # Dedicated environments with protections for publishing are strongly recommended.\n    environment:\n      name: pypi\n      # OPTIONAL: uncomment and update to include your PyPI project URL in the deployment status:\n      # url: https://pypi.org/p/YOURPROJECT\n\n    steps:\n      - name: Retrieve release distributions\n        uses: actions/download-artifact@v5\n        with:\n          name: release-dists\n          path: dist/\n\n      - name: Publish release distributions to PyPI\n        uses: pypa/gh-action-pypi-publish@6f7e8d9c0b1a2c3d4e5f6a7b8c9d0e1f2a3b4c5d\n```\n\n必要な PyPI 設定など、このワークフローの詳細については、「[PyPI での OpenID Connect の構成](/ja/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-pypi)」を参照してください。"}