Getting started¶
Note
Plugin does not install python and pip itself and use globally installed python (by default). It's easier to prepare python manually because python have good compatibility (from user perspective) and does not need to be updated often.
installation¶
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'ru.vyarus:gradle-use-python-plugin:3.0.0'
}
}
apply plugin: 'ru.vyarus.use-python'
OR
plugins {
id 'ru.vyarus.use-python' version '3.0.0'
}
Python & Pip¶
Make sure python and pip are installed:
python --version
pip --version
On *nix python
usually reference python2. For python3:
python3 --version
pip3 --version
Install python if required.
Docker¶
If you have docker installed, you can use python from docker container:
python.docker.use = true
In this case global python installation is not required.
Pip modules¶
If additional pip modules required configure them:
python.pip 'module1:1.0', 'module2:1.0'
or
python {
pip 'module1:1.0'
pip 'module2:1.0'
}
Important
Version ranges are not allowed for reproducible builds! (but, eventually, there would be problems in any case because of transitive dependencies)
Module features and VCS modules supported.
Behaviour¶
Default behaviour:
- if
virtualenv
module installed (or automatically installed): manage pip dependencies per project (env.gradle/python
created) - if no virtualenv - use user scope (
--user
pip flag): pip modules are installed only for current user (this avoid permission problems on linux)
To change defaults:
python.scope = VIRTUALENV
GLOBAL
- install modules globally (this may not work on linux due to permissions)USER
- use--user
flag to install for current user onlyVIRTUALENV_OR_USER
- defaultVIRTUALENV
- usevirtualenv
(if module not installed - error thrown)
Note
For multi-module projects, by default, plugin will create virtualenv inside the root project directory in order to share the same environment for all modules (but this could be changed).
Usage¶
Call python command:
task cmd(type: PythonTask) {
command = "-c print('sample')"
}
called: python -c print('sample')
on win and python -c exec("print('sample')")
on *nix (exec applied automatically for compatibility)
Note
Each PythonTask
would depend on checkPython
and pipInstall
tasks which would
prepare python environment before actual execution.
Call multi-line command:
task cmd(type: PythonTask) {
command = "-c \"import sys; print(sys.prefix)\""
}
called: python -c "import sys; print(sys.prefix)"
on win and python -c exec("import sys; print(sys.prefix)")
on *nix
Note
It is important to wrap script with space in quotes (otherwise parser will incorrectly parse arguments).
See command parsing specifics and env variables usage
String command is used for simplicity, but it could be array/collection of args:
task script(type: PythonTask) {
command = ['path/to/script.py', '1', '2']
}
Module¶
task mod(type: PythonTask) {
module = 'sample'
command = "mod args"
}
called: python -m sample mod args
Script¶
task script(type: PythonTask) {
command = "path/to/script.py 1 2"
}
called: python path/to/script.py 1 2
(arguments are optional, just for demo)