Run Python scripts from Gradle with isolated virtual environments and optional dependency installation.
- Creates and reuses a local
.venvin your task working directory - Installs
requirements.txtonly when file content changes (SHA-256 hash cache) - Runs scripts with arguments and surfaces stdout/stderr in Gradle logs
- Provides a single task name:
PythonRunnerTask
- Plugin ID:
name.jurgenei.gradle.python - Java package:
name.jurgenei.gradle.python - Registered task:
PythonRunnerTask
- JDK 17+ (tested with recent Gradle versions)
- Python 3 available on the build machine
- Gradle (or the provided wrapper)
plugins {
id 'name.jurgenei.gradle.python'
}
PythonRunnerTask {
script = file("scripts/run.py")
workDir = projectDir
requirements = file("requirements.txt") // optional
args = ["foo", "bar"] // optional
pythonExecutable = "/usr/bin/python3" // optional
}Then run:
./gradlew PythonRunnerTask| Property | Type | Required | Description |
|---|---|---|---|
script |
File |
Yes | Python script to execute. |
workDir |
File |
No | Working directory and location for .venv (defaults to script parent, then project dir). |
requirements |
File |
No | requirements.txt file to install; re-installs only when content hash changes. |
args |
List<String> |
No | Positional arguments passed to the script. |
pythonExecutable |
String |
No | Python interpreter used to create the virtual environment. |
When requirements is set, the plugin computes a SHA-256 hash of the file and stores it at .venv/.requirements.hash. If the hash is unchanged, dependency installation is skipped.
Run tests locally:
./gradlew testGitHub Actions is configured to run tests on every push (any branch) and pull request.
- Create a branch from
main. - Run
./gradlew testbefore opening a PR. - Submit a PR with a short change summary.
This project is licensed under the terms of the LICENSE file.