Skip to content

Commit b39eda6

Browse files
committed
feature: add a python-path output
Expose a `python-path` output containing the chosen Python executable path.
1 parent fff15a2 commit b39eda6

File tree

8 files changed

+98
-4
lines changed

8 files changed

+98
-4
lines changed

.github/workflows/test-pypy.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,22 @@ jobs:
3737
uses: actions/checkout@v2
3838

3939
- name: setup-python ${{ matrix.pypy }}
40+
id: setup-python
4041
uses: ./
4142
with:
4243
python-version: ${{ matrix.pypy }}
43-
44+
45+
- name: Check python-path
46+
run: |
47+
PATH_PYTHON=$(python -c 'import sys; print(sys.executable)')
48+
SETUP_PYTHON=$('${{ steps.setup-python.outputs.python-path }}' -c 'import sys; print(sys.executable)')
49+
if [ "${SETUP_PYTHON}" != "${PATH_PYTHON}" ]; then
50+
echo "Executable mismatch. python in PATH is ${PATH_PYTHON}; python-path (${{ steps.setup-python.outputs.python-path }}) is ${SETUP_PYTHON}"
51+
exit 1
52+
fi
53+
echo 'python-path: ${{ steps.setup-python.outputs.python-path }}'
54+
shell: bash
55+
4456
- name: PyPy and Python version
4557
run: python --version
4658

.github/workflows/test-python.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@ jobs:
2323
- name: Checkout
2424
uses: actions/checkout@v2
2525

26-
- name: setup default python
26+
- name: setup default python
27+
id: setup-python
2728
uses: ./
2829

30+
- name: Check python-path
31+
run: |
32+
$setupPython = (${{ steps.setup-python.outputs.python-path }} -c "import sys; print(sys.executable)")
33+
$pathPython = (python -c "import sys; print(sys.executable)")
34+
if ("$pathPython" -ne "$setupPython"){
35+
Write-Host "Executable mismatch. python in PATH is $pathPython; python-path (${{ steps.setup-python.outputs.python-path }}) is $setupPython"
36+
exit 1
37+
}
38+
Write-Host "python-path: ${{ steps.setup-python.outputs.python-path }}"
39+
shell: pwsh
40+
2941
- name: Validate version
3042
run: python --version
3143

@@ -45,10 +57,22 @@ jobs:
4557
uses: actions/checkout@v2
4658

4759
- name: setup-python ${{ matrix.python }}
60+
id: setup-python
4861
uses: ./
4962
with:
5063
python-version: ${{ matrix.python }}
5164

65+
- name: Check python-path
66+
run: |
67+
$setupPython = (${{ steps.setup-python.outputs.python-path }} -c "import sys; print(sys.executable)")
68+
$pathPython = (python -c "import sys; print(sys.executable)")
69+
if ("$pathPython" -ne "$setupPython"){
70+
Write-Host "Executable mismatch. python in PATH is $pathPython; python-path (${{ steps.setup-python.outputs.python-path }}) is $setupPython"
71+
exit 1
72+
}
73+
Write-Host "python-path: ${{ steps.setup-python.outputs.python-path }}"
74+
shell: pwsh
75+
5276
- name: Validate version
5377
run: |
5478
$pythonVersion = (python --version)
@@ -74,10 +98,22 @@ jobs:
7498
uses: actions/checkout@v2
7599

76100
- name: setup-python 3.9.0-beta.4
101+
id: setup-python
77102
uses: ./
78103
with:
79104
python-version: '3.9.0-beta.4'
80105

106+
- name: Check python-path
107+
run: |
108+
$setupPython = (${{ steps.setup-python.outputs.python-path }} -c "import sys; print(sys.executable)")
109+
$pathPython = (python -c "import sys; print(sys.executable)")
110+
if ("$pathPython" -ne "$setupPython"){
111+
Write-Host "Executable mismatch. python in PATH is $pathPython; python-path (${{ steps.setup-python.outputs.python-path }}) is $setupPython"
112+
exit 1
113+
}
114+
Write-Host "python-path: ${{ steps.setup-python.outputs.python-path }}"
115+
shell: pwsh
116+
81117
- name: Validate version
82118
run: |
83119
$pythonVersion = (python --version)

.github/workflows/workflow.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,13 @@ jobs:
8989
python-version: 3.8.1
9090
- name: Verify 3.8.1
9191
run: python __tests__/verify-python.py 3.8.1
92+
93+
- name: Run with setup-python 3.10
94+
id: cp310
95+
uses: ./
96+
with:
97+
python-version: "3.10"
98+
- name: Verify 3.10
99+
run: python __tests__/verify-python.py 3.10
100+
- name: Run python-path sample 3.10
101+
run: pipx run --python '${{ steps.cp310.outputs.python-path }}' nox --version

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ jobs:
137137
```
138138
More details on PyPy syntax and examples of using preview / nightly versions of PyPy can be found in the [Available versions of PyPy](#available-versions-of-pypy) section.
139139
140+
An output is available with the absolute path of the python interpreter executable if you need it:
141+
```yaml
142+
jobs:
143+
build:
144+
runs-on: ubuntu-latest
145+
steps:
146+
- uses: actions/checkout@v3
147+
- uses: actions/setup-python@v3
148+
id: cp310
149+
with:
150+
python-version: "3.10"
151+
- run: pipx run --python '${{ steps.cp310.outputs.python-path }}' nox --version
152+
```
153+
140154
# Getting started with Python + Actions
141155
142156
Check out our detailed guide on using [Python with GitHub Actions](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-python-with-github-actions).

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ outputs:
2121
description: "The installed python version. Useful when given a version range as input."
2222
cache-hit:
2323
description: 'A boolean value to indicate a cache entry was found'
24+
python-path:
25+
description: "The absolute path to the python interpreter executable."
2426
runs:
2527
using: 'node16'
2628
main: 'dist/setup/index.js'

dist/setup/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52375,12 +52375,15 @@ function findPyPyVersion(versionSpec, architecture) {
5237552375
}
5237652376
const pipDir = utils_1.IS_WINDOWS ? 'Scripts' : 'bin';
5237752377
const _binDir = path.join(installDir, pipDir);
52378+
const binaryExtension = utils_1.IS_WINDOWS ? '.exe' : '';
52379+
const pythonPath = path.join(utils_1.IS_WINDOWS ? installDir : _binDir, `python${binaryExtension}`);
5237852380
const pythonLocation = pypyInstall.getPyPyBinaryPath(installDir);
5237952381
core.exportVariable('pythonLocation', pythonLocation);
5238052382
core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig');
5238152383
core.addPath(pythonLocation);
5238252384
core.addPath(_binDir);
5238352385
core.setOutput('python-version', 'pypy' + resolvedPyPyVersion.trim());
52386+
core.setOutput('python-path', pythonPath);
5238452387
return { resolvedPyPyVersion, resolvedPythonVersion };
5238552388
});
5238652389
}
@@ -57027,8 +57030,11 @@ function useCpythonVersion(version, architecture) {
5702757030
core.exportVariable('LD_LIBRARY_PATH', pyLibPath + libPath);
5702857031
}
5702957032
}
57033+
const _binDir = binDir(installDir);
57034+
const binaryExtension = utils_1.IS_WINDOWS ? '.exe' : '';
57035+
const pythonPath = path.join(utils_1.IS_WINDOWS ? installDir : _binDir, `python${binaryExtension}`);
5703057036
core.addPath(installDir);
57031-
core.addPath(binDir(installDir));
57037+
core.addPath(_binDir);
5703257038
if (utils_1.IS_WINDOWS) {
5703357039
// Add --user directory
5703457040
// `installDir` from tool cache should look like $RUNNER_TOOL_CACHE/Python/<semantic version>/x64/
@@ -57042,6 +57048,7 @@ function useCpythonVersion(version, architecture) {
5704257048
// On Linux and macOS, pip will create the --user directory and add it to PATH as needed.
5704357049
const installed = versionFromPath(installDir);
5704457050
core.setOutput('python-version', installed);
57051+
core.setOutput('python-path', pythonPath);
5704557052
return { impl: 'CPython', version: installed };
5704657053
});
5704757054
}

src/find-pypy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ export async function findPyPyVersion(
4848

4949
const pipDir = IS_WINDOWS ? 'Scripts' : 'bin';
5050
const _binDir = path.join(installDir, pipDir);
51+
const binaryExtension = IS_WINDOWS ? '.exe' : '';
52+
const pythonPath = path.join(
53+
IS_WINDOWS ? installDir : _binDir,
54+
`python${binaryExtension}`
55+
);
5156
const pythonLocation = pypyInstall.getPyPyBinaryPath(installDir);
5257
core.exportVariable('pythonLocation', pythonLocation);
5358
core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig');
5459
core.addPath(pythonLocation);
5560
core.addPath(_binDir);
5661
core.setOutput('python-version', 'pypy' + resolvedPyPyVersion.trim());
62+
core.setOutput('python-path', pythonPath);
5763

5864
return {resolvedPyPyVersion, resolvedPythonVersion};
5965
}

src/find-python.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ export async function useCpythonVersion(
8383
}
8484
}
8585

86+
const _binDir = binDir(installDir);
87+
const binaryExtension = IS_WINDOWS ? '.exe' : '';
88+
const pythonPath = path.join(
89+
IS_WINDOWS ? installDir : _binDir,
90+
`python${binaryExtension}`
91+
);
8692
core.addPath(installDir);
87-
core.addPath(binDir(installDir));
93+
core.addPath(_binDir);
8894

8995
if (IS_WINDOWS) {
9096
// Add --user directory
@@ -106,6 +112,7 @@ export async function useCpythonVersion(
106112

107113
const installed = versionFromPath(installDir);
108114
core.setOutput('python-version', installed);
115+
core.setOutput('python-path', pythonPath);
109116

110117
return {impl: 'CPython', version: installed};
111118
}

0 commit comments

Comments
 (0)