setup-python

repository·main·Indexed 24 days ago

https://github.com/actions/setup-python

A GitHub Action that installs Python, PyPy, or GraalPy versions and manages dependency caching for pip, pipenv, and poetry. It supports specifying versions via python-version or python-version-file, configuring interpreter architectures (x86, x64, arm64), and enabling free-threaded Python (3.13+). The action includes built-in caching mechanisms and supports matrix testing across multiple OS and Python versions.

Tokens
6.7K
Snippets
25
Records
31
Agent score
80%

What's inside setup-python

  1. Available Python and PyPy versions

    main

    Python

    • Preinstalled: Available on GitHub-hosted runners. For minor versions, only the latest patch is typically preinstalled (e.g., 3.12 gets 3.12.x). Use -dev (e.g., 3.14-dev) for the latest patch including alpha/beta releases.
    • Downloadable: Available via actions/python-versions from GitHub Releases.

    PyPy

    • Preinstalled: Available on GitHub-hosted runners. Note that cached versions may have a 1-2 week delay. To use a brand new release immediately, specify the exact version (e.g., pypy3.10-v7.3.17).
    • Downloadable: Available from the official PyPy site. Versions < 7.3.3 cannot be installed on-flight.

    GraalPy

    • Downloadable: Available from the official GraalPy repository.
  2. Avoid rate limits on GHES with `setup-python`

    main

    When using GitHub Enterprise Server (GHES), setup-python downloads distributions from actions/python-versions on github.com.

    • Unauthenticated Requests: By default, these are limited to 60 requests per hour per IP. This can be problematic for GHES runners sharing an IP via NAT.
    • Automatic Fallback: If the rate limit is reached, the action automatically leverages the raw API to retrieve the version-manifest, which does not impose a rate limit and allows for unrestricted consumption.
  3. Configure `setup-python` for macOS self-hosted runners

    main

    Python packages for macOS are non-relocatable due to fixed shared library paths and must be installed in /Users/runner/hostedtoolcache.

    Before using setup-python on a macOS self-hosted runner, you must:

    1. Create the directory /Users/runner/hostedtoolcache.
    2. Grant the runner write access to that directory (e.g., via chown).

    Example command to grant access: sudo chown runner-user:runner-group /Users/runner/hostedtoolcache.

    sudo chown runner-user:runner-group /Users/runner/hostedtoolcache
  4. Install multiple Python or PyPy versions

    main

    The python-version input accepts a list of versions. When multiple versions are provided, the action installs all of them, and the last version in the list is set as the default version for the environment.

    # Multiple Python versions
    - uses: actions/setup-python@v7
      with:
        python-version: |
            3.11
            3.12
            3.13
    
    # Multiple PyPy versions
    - uses: actions/setup-python@v7
      with:
        python-version: |
            pypy-3.10-v7.3.x
            pypy3.10-nightly
            pypy3.9
  5. Cache Python dependencies with setup-python

    main

    You can enable built-in dependency caching for pip and pipenv to speed up your GitHub Actions workflows. This feature simplifies your YAML pipelines by removing the need for manual actions/cache configuration for standard use cases.

    Supported Package Managers

    • pip: Enables caching for pip dependencies. It searches for dependency files (like requirements.txt) and uses their hash as part of the cache key.
    • pipenv: Enables caching for pipenv dependencies. It searches for Pipfile.lock and includes the Python version in the cache key to prevent version mismatches.

    Configuration Inputs

    • cache: Specifies the package manager to use. Options are pip, pipenv, or an empty string '' to disable caching (default).
    • cache-dependency-path: An array or regex of dependency files (relative to the repository root). If wildcards are used, the action calculates a common hash for all matching files.

    Important Limitations

    • No dependency-specific caching per job: The action does not support different caches for different jobs. If different dependencies are installed across jobs, the cache from the first job will be used in the second. To manage this, create separate caches for jobs with different requirements.
    • Customization: This integration covers ~90% of basic use cases. If you require highly flexible or custom caching logic, use actions/cache directly.
    - uses: actions/setup-python@v4
      with:
        python-version: 3.9
        cache: pip
  6. Specify Python version from a file using `python-version-file`

    main

    Instead of hardcoding a version in your workflow, you can use the python-version-file input to point to a file containing the version specification.

    Supported file types include:

    • .python-version (asdf standard)
    • pyproject.toml
    • .tool-versions (asdf standard)
    • Pipfile

    Note: If both python-version and python-version-file are provided, python-version-file is ignored.

    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-python@v7
      with:
        python-version-file: '.python-version'
    - run: python my_script.py
  7. Perform Matrix Testing with Python versions

    main

    You can use GitHub Actions matrix strategy to test your code against multiple Python or PyPy versions. You can also use the exclude keyword to skip specific combinations (e.g., skipping a specific Python version on a specific OS).

    jobs:
      build:
        runs-on: ${{ matrix.os }}
        strategy:
          fail-fast: false
          matrix:
            os: [ubuntu-latest, macos-latest, windows-latest]
            python-version: ['3.9', '3.10', '3.11', 'pypy3.9']
            exclude:
              - os: macos-latest
                python-version: '3.9'
              - os: windows-latest
                python-version: '3.9'
        steps:
          - uses: actions/checkout@v7
          - name: Set up Python
            uses: actions/setup-python@v7
            with:
              python-version: ${{ matrix.python-version }}
  8. Configure `setup-python` for Ubuntu self-hosted runners

    main

    To use setup-python on Ubuntu self-hosted runners:

    • Tool Cache Location: By default, tools are installed to the directory defined by RUNNER_TOOL_CACHE. To change this, set the AGENT_TOOLSDIRECTORY environment variable.
      • Temporary (current shell): export AGENT_TOOLSDIRECTORY=/path/to/folder
      • Permanent (service): Add AGENT_TOOLSDIRECTORY=/path/to/folder to an .env file in the runner's directory.
    • Permissions: The user starting the runner must have write access to the tool cache directory. You can grant access by ensuring the runner user owns the directory or is in the owning group. A quick way to grant access is using chown: sudo chown runner-user:runner-group /path/to/folder.
    sudo chown runner-user:runner-group /path/to/folder
  9. Configure `setup-python` for Windows self-hosted runners

    main

    To use setup-python on Windows self-hosted runners, ensure the following requirements are met:

    • Administrator Privileges: The runner must run with administrator privileges to install new Python versions.
    • Service Permissions: If running as a service, the service account (e.g., NT AUTHORITY\NETWORK SERVICE) must have appropriate write permissions.
    • 7zip: Must be installed and added to the PATH for extracting Python files.
    • MSI Installers: The action uses MSI installers. Note that the 3.8 MSI installer prevents installing another 3.8 version; manually remove previous 3.8 versions via "Apps & Features" if installation fails.
    • Tool Cache Location: By default, tools are installed to the directory defined by RUNNER_TOOL_CACHE. You can change this location by setting the AGENT_TOOLSDIRECTORY environment variable.
  10. Specify a Python version using `python-version`

    main

    You can use the python-version input to specify which Python version to install. Depending on the syntax used, the setup behavior changes:

    • Exact version: Specify major, minor, and patch (e.g., 3.12.6) to avoid breaking changes from patch updates. This may take longer as it might require a download.
    • Major/Minor version: Specify only major and minor (e.g., 3.13) to use the most recent patch version. This is typically faster because a patch version is often pre-installed on the runner.
    • Prerelease: Use a prerelease tag (e.g., 3.14.0-alpha.1) to install specific preview versions.
    • Dev syntax: Use x.y-dev (e.g., 3.14-dev) to install the latest patch, alpha, beta, or release candidate.
    • Semver ranges: Use standard semver syntax (e.g., '>=3.9 <3.14', '3.x', or '3.13.0-alpha - 3.13.0') to satisfy version requirements.
    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-python@v7
      with:
        python-version: '3.12.6' 
    - run: python my_script.py
  11. Cache pip, pipenv, or poetry dependencies

    main

    The action provides built-in caching for pip, pipenv, and poetry. Caching is disabled by default; you must set the cache input to enable it.

    • pip: Caches the global cache directory. Looks for requirements.txt by default.
    • pipenv: Caches the virtualenv directory. Looks for Pipfile.lock by default.
    • poetry: Caches virtualenv directories (one for each project found). Looks for poetry.lock by default.

    Use cache-dependency-path if your dependency files are in subdirectories or if you use multiple files and want to specify which one should be used for the cache key hash.

    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-python@v7
      with:
        python-version: '3.13'
        cache: 'pip' # caching pip dependencies
    - run: pip install -r requirements.txt
  12. Recommended permissions for setup-python

    main

    To ensure the action can properly check out code and install dependencies, it is recommended to set the following permissions in your workflow:

    permissions:
      contents: read # access to check out code and install dependencies