| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
If there is a specific version of Python that you need and you don't want to worry about any potential breaking changes due to patch updates (going from 3.12.6 to 3.12.7 for example), you should specify the exact major, minor, and patch version (such as 3.12.6):
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.12.6'
- run: python my_script.pyYou can specify only a major and minor version if you are okay with the most recent patch version being used:
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13'
- run: python my_script.pyYou can specify the version with prerelease tag to download and set up an accurate pre-release version of Python:
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.14.0-alpha.1'
- run: python my_script.pyIt's also possible to use x.y-dev syntax to download and set up the latest patch version of Python, alpha, beta and rc (release candidate) releases included. (for specified major & minor versions):
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.14-dev'
- run: python my_script.pyYou can specify the free threading version of Python by setting the freethreaded input to true or by using the special t suffix in some cases. You can use the t suffix when specifying the major and minor version (e.g., 3.13t), with a patch version (e.g., 3.13.1t), or with the x.y-dev syntax (e.g., 3.14t-dev). Free threaded Python is only available starting with the 3.13 release.
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13t'
- run: python my_script.pyNote that the t suffix is not semver syntax. If you wish to specify a range, you must use the freethreaded input instead of the t suffix.
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '>=3.13'
freethreaded: true
- run: python my_script.pyYou can also use several types of ranges that are specified in semver, for instance:
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '>=3.9 <3.14'
- run: python my_script.pysteps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13.0-alpha - 3.13.0'
- run: python my_script.pysteps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.x'
- run: python my_script.pyPlease refer to the Advanced range syntax section of the semver to check other available range syntaxes.
The version of PyPy should be specified in the format pypy<python_version>[-v<pypy_version>] or pypy-<python_version>[-v<pypy_version>]. The -v<pypy_version> parameter is optional and can be skipped. The latest PyPy version will be used in this case.
pypy3.10 or pypy-3.10 # the latest available version of PyPy that supports Python 3.10 pypy3.9 or pypy-3.9 # the latest available version of PyPy that supports Python 3.9 pypy3.7-v7.3.3 or pypy-3.7-v7.3.3 # Python 3.7 and PyPy 7.3.3 pypy3.7-v7.x or pypy-3.7-v7.x # Python 3.7 and the latest available PyPy 7.x pypy3.7-v7.3.3rc1 or pypy-3.7-v7.3.3rc1 # Python 3.7 and preview version of PyPy pypy3.7-nightly or pypy-3.7-nightly # Python 3.7 and nightly PyPy
Download and set up PyPy:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
- 'pypy3.10' # the latest available version of PyPy that supports Python 3.10
- 'pypy3.10-v7.3.17' # Python 3.10 and PyPy 7.3.17
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- run: python my_script.pyMore details on the syntax for PyPy can be found in the Available versions of PyPy section.
The python-version input can get multiple python/pypy versions. The last specified version will be used as a default one.
Download and set up multiple Python versions:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: |
3.11
3.12
3.13
- run: python my_script.pyDownload and set up multiple PyPy versions:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: |
pypy-3.10-v7.3.x
pypy3.10-nightly
pypy3.9
- run: python my_script.pyDownload and set up multiple Python/PyPy versions:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: |
3.11
3.12
pypy3.10-nightly
pypy3.10
3.13
- run: python my_script.pyUsing setup-python it's possible to use the matrix syntax to install several versions of Python or PyPy:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.x', 'pypy3.8', 'pypy3.9' ]
name: Python ${{ matrix.python-version }} sample
steps:
- uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
architecture: x64
- run: python my_script.pyExclude a specific Python version:
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@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- name: Display Python version
if: ${{ matrix.python-version != 'pypy3.9' }} # Use single quotes in expressions for input `python-version`
run: python --versionsetup-python action can read Python or PyPy version from a version file. python-version-file input is used for specifying the path to the version file. If the file that was supplied to python-version-file input doesn't exist, the action will fail with error.
In case both python-version and python-version-file inputs are supplied, the python-version-file input will be ignored due to its lower priority. The .tool-versions file supports version specifications in accordance with asdf standards, adhering to Semantic Versioning (semver).
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version-file: '.python-version' # Read python version from a file .python-version
- run: python my_script.pysteps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version-file: 'pyproject.toml' # Read python version from a file pyproject.toml
- run: python my_script.pysteps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version-file: '.tool-versions' # Read python version from a file .tool-versions
- run: python my_script.pysteps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version-file: 'Pipfile' # Read python version from a file Pipfile
- run: python my_script.pyThe check-latest flag defaults to false. Use the default or set check-latest to false if you prefer stability and if you want to ensure a specific Python or PyPy version is always used.
If check-latest is set to true, the action first checks if the cached version is the latest one. If the locally cached version is not the most up-to-date, a Python or PyPy version will then be downloaded. Set check-latest to true if you want the most up-to-date Python or PyPy version to always be used.
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13'
check-latest: true
- run: python my_script.pySetting check-latest to true impacts performance as downloading Python or PyPy versions is slower than using cached versions.
Caching pipenv dependencies:
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13'
cache: 'pipenv'
- name: Install pipenv
run: curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python
- run: pipenv installCaching poetry dependencies:
steps:
- uses: actions/checkout@v6
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v6
with:
python-version: '3.13'
cache: 'poetry'
- run: poetry install
- run: poetry run pytestNote: If the setup-python version does not match the version specified in pyproject.toml and the python version in pyproject.toml is less than the runner's python version, poetry install will default to using the runner's Python version.
Using a list of file paths to cache dependencies
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13'
cache: 'pipenv'
cache-dependency-path: |
server/app/Pipfile.lock
__test__/app/Pipfile.lock
- name: Install pipenv
run: curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python
- run: pipenv installUsing wildcard patterns to cache dependencies
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13'
cache: 'pip'
cache-dependency-path: '**/requirements-dev.txt'
- run: pip install -r subdirectory/requirements-dev.txtUsing a list of wildcard patterns to cache dependencies
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13'
cache: 'pip'
cache-dependency-path: |
**/setup.cfg
**/requirements*.txt
- run: pip install -e . -r subdirectory/requirements-dev.txtCaching projects that use setup.py:
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: '3.13'
cache: 'pip'
cache-dependency-path: setup.py
- run: pip install -e .
# Or pip install -e '.[test]' to install test dependenciesUsing python-version output, it's possible to get the precise Python or PyPy version installed by the action. This output is useful when the input python-version is given as a range (e.g. 3.9.0 - 3.12.0, 3.x ), but down the line you need to operate (such as in an if: statement) with the exact installed version (e.g. 3.12.0).
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
id: cp312
with:
python-version: "3.9.0 - 3.12.0"
- run: echo '${{ steps.cp312.outputs.python-version }}'python-path output is available to get the absolute path of the Python or PyPy interpreter executable:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
id: cp313
with:
python-version: "3.13"
- run: pipx run --python '${{ steps.cp313.outputs.python-path }}' nox --versioncache-hit output is available with a boolean value that indicates whether a cache hit occurred on the primary key:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
id: cp313
with:
python-version: "3.13.0"
cache: "poetry"
- run: echo '${{ steps.cp313.outputs.cache-hit }}' # true if cache-hit occurred on the primary keyThese environment variables become available after setup-python action execution:
| Env.variable | Description |
|---|---|
| pythonLocation | Contains the absolute path to the folder where the requested version of Python, PyPy, or GraalPy is installed. Executable location by implementation: • CPython – $pythonLocation/bin/python (Linux/macOS), $pythonLocation/python.exe (Windows) • PyPy – $pythonLocation/bin/python (Linux/macOS), $pythonLocation/python.exe (Windows) • GraalPy – $pythonLocation/bin/python (Linux/macOS) Note: CPython versions include a symlink or copy of the Python executable at the root, while PyPy and GraalPy retain upstream directory layouts. |
| Python_ROOT_DIR | https://cmake.org/cmake/help/latest/module/FindPython.html#module:FindPython |
| Python2_ROOT_DIR | https://cmake.org/cmake/help/latest/module/FindPython2.html#module:FindPython2 |
| Python3_ROOT_DIR | https://cmake.org/cmake/help/latest/module/FindPython3.html#module:FindPython3 |
The update-environment flag defaults to true. With this setting, the action will add/update environment variables (e.g. PATH, PKG_CONFIG_PATH, pythonLocation) for Python or PyPy to just work out of the box.
If update-environment is set to false, the action will not add/update environment variables. This can prove useful if you only want the side-effect to ensure that Python or PyPy is installed and rely on the python-path output to run the executable. Such a requirement on side-effect could be because you don't want your composite action messing with your user's workflows.
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
id: cp313
with:
python-version: '3.13'
update-environment: false
- run: ${{ steps.cp313.outputs.python-path }} my_script.pysetup-python is able to configure Python from two sources:
Note: Python versions used in this action are generated in the python-versions repository. For macOS and Ubuntu images, python versions are built from the source code. For Windows, the python-versions repository uses installation executable. For more information please refer to the python-versions repository.
setup-python is able to configure PyPy from two sources:
Preinstalled versions of PyPy in the tool cache on GitHub-hosted runners
Downloadable PyPy versions from the official PyPy site.
setup-python is able to download GraalPy versions from the official GraalPy repository.
GitHub hosted runners have a tool cache that comes with a few versions of Python + PyPy already installed. This tool cache helps speed up runs and tool setup by not requiring any new downloads. There is an environment variable called RUNNER_TOOL_CACHE on each runner that describes the location of the tool cache with Python and PyPy installed. setup-python works by taking a specific version of Python or PyPy from this tool cache and adding it to PATH.
| Location | |
|---|---|
| Tool cache Directory | RUNNER_TOOL_CACHE |
| Python tool cache | RUNNER_TOOL_CACHE/Python/* |
| PyPy tool cache | RUNNER_TOOL_CACHE/PyPy/* |
GitHub runner images are set up in actions/runner-images. During the setup, the available versions of Python and PyPy are automatically downloaded, set up and documented.
Python distributions are only available for the same environments that GitHub Actions hosted environments are available for. If you are using an unsupported version of Ubuntu such as 19.04 or another Linux distribution such as Fedora, setup-python may not work.
If you have a supported self-hosted runner and you would like to use setup-python, there are a few extra things you need to make sure are set up so that new versions of Python can be downloaded and configured on your runner.
By default runner downloads and installs tools into the folder set up by RUNNER_TOOL_CACHE environment variable. The environment variable called AGENT_TOOLSDIRECTORY can be set to change this location for Windows self-hosted runners.
If you are experiencing problems while configuring Python on your self-hosted runner, turn on step debugging to see additional logs.
By default, the runner downloads and installs tools into the folder set up by RUNNER_TOOL_CACHE environment variable. The environment variable called AGENT_TOOLSDIRECTORY can be set to change this location for Ubuntu self-hosted runners:
If you're using a non-default tool cache directory be sure that the user starting the runner has write permission to the new tool cache directory. To check the current user and group that the runner belongs, type ls -l inside the runner's root directory.
The runner can be granted write access to any directory using a few techniques:
If your runner is configured as a service and you run into problems, make sure the user that the service is running as is correct. For more information, you can check the status of your self-hosted runner.
The Python packages for macOS that are downloaded from actions/python-versions are originally compiled from the source in /Users/runner/hostedtoolcache. Due to the fixed shared library path, these Python packages are non-relocatable and require to be installed only in /Users/runner/hostedtoolcache. Before the use of setup-python on the macOS self-hosted runner:
You can check the current user and group that the runner belongs to by typing ls -l inside the runner's root directory.
The runner can be granted write access to the /Users/runner/hostedtoolcache directory using a few techniques:
If your runner is configured as a service and you run into problems, make sure the user that the service is running as is correct. For more information, you can check the status of your self-hosted runner.
setup-python comes pre-installed on the appliance with GHES if Actions is enabled. When dynamically downloading Python distributions, setup-python downloads distributions from actions/python-versions on github.com (outside of the appliance). These calls to actions/python-versions are by default made via unauthenticated requests, which are limited to 60 requests per hour per IP. If more requests are made within the time frame, then the action leverages the raw API to retrieve the version-manifest. This approach does not impose a rate limit and hence facilitates unrestricted consumption. This is particularly beneficial for GHES runners, which often share the same IP due to Network Address Translation (NAT), to avoid the quick exhaustion of the unauthenticated rate limit.
If the runner is not able to access github.com, any Python versions requested during a workflow run must come from the runner's tool cache. See "Setting up the tool cache on self-hosted runners without internet access" for more information.
The allow-prereleases flag defaults to false. If allow-prereleases is set to true, the action will allow falling back to pre-release versions of Python when a matching GA version of Python is not available. This allows for example to simplify reuse of python-version as an input of nox for pre-releases of Python by not requiring manipulation of the 3.y-dev specifier. For CPython, allow-prereleases will only have effect for x.y version range (e.g. 3.14). Let's say that in the past, when python 3.14 was not yet generally available, the following workflow would have fallback to the most recent pre-release of python 3.14:
jobs:
test:
name: ${{ matrix.os }} / ${{ matrix.python_version }}
runs-on: ${{ matrix.os }}-latest
strategy:
fail-fast: false
matrix:
os: [Ubuntu, Windows, macOS]
python_version: ["3.14"]
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: "${{ matrix.python_version }}"
allow-prereleases: true
- run: pipx run nox --error-on-missing-interpreters -s tests-${{ matrix.python_version }}The pip-version input allows you to specify the desired version of Pip to use with the standard Python version. The version of Pip should be specified in the format major, major.minor, or major.minor.patch (for example: 25, 25.1, or 25.0.1).
steps:
- uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
pip-version: '25.0.1'
- name: Display Pip version
run: pip --versionThe pip-version input is supported only with standard Python versions. It is not available when using PyPy or GraalPy.
Using a specific or outdated version of pip may result in compatibility or security issues and can cause job failures. For best practices and guidance, refer to the official pip documentation.
The pip-install input allows you to install dependencies as part of the Python setup step.
steps:
- uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
pip-install: -r requirements.txtNote: This feature is intended for standard pip-based dependency installations. For complex workflows, or alternative package managers (e.g., poetry, pipenv), we recommend using separate steps to maintain clarity and flexibility.
The pip-install input mirrors the flexibility of a standard pip install command and supports most of its arguments.
| Back | FazBrowse Home | New Git URL |