在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):xvik/gradle-use-python-plugin开源软件地址(OpenSource Url):https://github.com/xvik/gradle-use-python-plugin开源编程语言(OpenSource Language):Groovy 100.0%开源软件介绍(OpenSource Introduction):Gradle use-python pluginAboutPlugin 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. The only plugin intention is to simplify python usage from gradle. By default, plugin creates python virtualenv inside the project and installs all modules there so each project has its own python (copy) and could not be affected by other projects or system changes. Features:
Summary
Possible pip issue warning (linux/macos)If This is a known issue related to incorrectly patched pip packages in some distributions. Setupbuildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'ru.vyarus:gradle-use-python-plugin:2.3.0'
}
}
apply plugin: 'ru.vyarus.use-python' OR plugins {
id 'ru.vyarus.use-python' version '2.3.0'
} CompatibilityPlugin compiled for java 8, compatible with java 11
SnapshotsSnapshots may be used through JitPack
For gradle before 6.0 use buildscript {
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'ru.vyarus:gradle-use-python-plugin:2450c7e881'
}
}
apply plugin: 'ru.vyarus.use-python' For gradle 6.0 and above:
Python & PipMake sure python and pip are installed: python --version
pip --version On *nix python3 --version
pip3 --version Windows installDownload and install python manually or use chocolately: choco install python In Windows 10 python 3.9 could be installed from Windows Store: just type 'python' in console and windows will open Windows Store's python page. No additional actions required after installation. Note that windows store python will require minium virtualenv 20.0.11 (or above). (if virtualenv not yet installed then no worry - plugin will install the correct version) Linux/Macos installOn most *nix distributions python is already installed, but often without pip. Install pip if required (ubuntu example): sudo apt-get install python3-pip Make sure the latest pip installed (required to overcome some older pip problems): pip3 install -U pip To install exact pip version: pip3 install -U pip==20.0.11 Note that on ubuntu pip installed with Automatic pip upgradeAs described above, there are different ways of pip installation in linux and, more important, admin permissions are required to upgrade global pip. So it is impossible to upgrade pip from the plugin (in all cases). But, it is possible inside virtualenv or user (--user) scope. Note that plugin creates virtualenv by default (per project independent python environment). So, in order to use newer pip simply put it as first dependency:
Here project virtualenv will be created with global pip and newer pip version installed inside environment. Packages installation is sequential, so all other packages will be installed with newer pip (each installation is independent pip command). The same will work for user scope: When applying this trick, consider minimal pip version declared in configuration
( Automatic python installPython is assumed to be used as java: install and forget. It perfectly fits user use case: install python once and plugin will replace all manual work on project environment setup. It is also easy to configure python on CI (like travis). If you want automatic python installation, try looking on JetBrain's python-envs plugin. But be careful because it has some caveats (for example, on windows python could be installed automatically just once and requires manual un-installation). Multi-module projectsWhen used in multi-module project, plugin will create virtualenv inside the root project directory in order to share the same environment for all modules. Travis CI configurationTo make plugin work on travis you'll need to install python3 packages: language: java
dist: bionic
jdk: openjdk8
addons:
apt:
packages:
- python3
- python3-pip
- python3-setuptools
before_install:
- python3 --version
- pip3 --version
- pip3 install -U pip It will be python 3.6 by default (for bionic). Appveyour CI configurationTo make plugin work on appveyour you'll need to add python to path: environment:
matrix:
- JAVA_HOME: C:\Program Files\Java\jdk1.8.0
PYTHON: "C:\\Python36-x64"
install:
- set PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%
- python --version Now plugin would be able to find python binary. To use python 3.9 you'll need to switch image: image: Visual Studio 2019 See available pythons matrix for more info. UsageDeclare required modules (optional): python.pip 'module1:1.0', 'module2:1.0' or python {
pip 'module1:1.0'
pip 'module2:1.0'
} Module format is: python.pip 'module1:2.0', 'module2:1.0', 'module1:1.0' Will install version 1.0 of module1 because it was the latest declaration. "Module overrides" works for all declaration types (see below): the latest declared module version always wins. Dependencies are installed with Note that by default dependencies are installed inside project specific virtualenv (project specific copy of python environment). Behaviour matrix for possible
Note that
Pip module extra featuresYou can declare modules with extra features in module name to install special version of module (with enabled features): python.pip 'requests[socks,security]:2.18.4' IMPORTANT: it is impossible to track if this "variation" of module is installed, so
plugin performs up-to-date check for such modules by name only (for example,
if 'requests==2.18.4' is already installed). For most cases, this is suitable behaviour because, by default,
modules are installed in virtualenv and so you will always have correct module installed.
For other cases, you can disable up-to-date checks (delegate all dependencies logic to pip): VCS pip modulesYou can declare vcs modules: modules installed directly from version control (e.g. git, svn). Format:
For example: python.pip 'git+https://github.com/ictxiangxin/boson/@b52727f7170acbedc5a1b4e1df03972bd9bb85e3#egg=boson-0.9' Declares module
WARNING: module version part assumed to follow the last dash, so if you specify version like
Vcs module installation is: source checkout and module build (using setup.py). You may need to specify subdirectory
as To avoid installation problems, package version is not used for actual installation (in spite of the fact that its official
convention, it doesnt work in some cases). For example, module above will be installed as (no pip install git+https://github.com/ictxiangxin/boson/@b52727f7170acbedc5a1b4e1df03972bd9bb85e3#egg=boson All pip supported vcs could be used: git, svn, hg, bzr If up-to-date logic, implemented by NOTE: since pip 20, compiled vcs module is cached (before it was build on each execution), but
it is possible to disable cache (for all modules) with Extra pip repositoriesTo add additional pip repositories (probably self-hosted): python {
extraIndexUrls = ["http://extra-url.com", "http://extra-url.com"]
} or with shortcut method (shortcut may be used multiple times): python {
extraIndexUrls "http://extra-url.com", "http://extra-url2.com"
} Extra urls will be applied as --extra-index-url
flag for pip commands supporting it: install, download, list and wheel. By default, it only affects In case of ssl problems (stale or self-signed certificated), mark domains as trusted: python {
trustedHosts = ["extra-url.com"]
} or python {
trustedHosts "extra-url.com"
} Applied as --trusted-host
option only for NOTE: if, for some reason, you don't want to specify it for all pip tasks, you can configure exact task,
for example: Extra pip install optionsIt is impossible to support directly all possible pipInstall.options('--upgrade-strategy', 'only-if-needed') Shortcut method above may be called multiple times: pipInstall.options('--a', 'value')
pipInstall.options('--b', 'value') Or you can use property directly: pipInstall.options = ['--a', 'value', '--b', 'value'] VirtualenvWhen you declare any pip modules, plugin will try to use virtualenv in order to install required modules locally (for current project only). If virtualenv is not installed - it will be installed automatically in python.installVirtualenv = false Plugin installs exact pip version declared in In any case, plugin checks if virtualenv is already installed and use it to create local environment
(if not, then fall back to python.scope = USER // or GLOBAL With USER (or GLOBAL) scope, virtualenv will not be used, even if it's already created in project (plugin will ignore it and use global python). If you already use virtualenv in your project (have created manually environment), then simply point plugin to use it: python.envPath = 'path/to/your/env' It will automatically change NOTE: plugin will not create environment if you don't use any modules. If you still want to use project specific environment
(without declared pip modules) then create it manually: IMPORTANT: virtualenv creates local python copy (by default in To copy environment instead of symlinking (default) set (--always-copy): python.envCopy = true ScopePip dependencies could be installed per project, for current user (~/) or globally. Default behaviour:
To change defaults: python.scope = VIRTUALENV
Note that values may be declared without quotes because it's an enum which values are
declared as project ext properties ( Complete behaviour matrix see above Check modules updatesTo quick check if new versions are available for the registered pip modules
use
Note that it will not show versions for transitive modules, only
for modules specified directly in To see all available updates (without filtering): pipUpdates.all = true NOTE: If you see an error like
then update pip:
Call pythonCall python command: task cmd(type: PythonTask) {
command = "-c print('sample')"
} called: Call multi-line command: task cmd(type: PythonTask) {
command = "-c \"import sys; print(sys.prefix)\""
} called: NOTE: it is important to wrap script with space in quotes (otherwise parser will incorrectly parse arguments). Call module: task mod(type: PythonTask) {
module = 'sample'
command = "mod args"
} called: Call script: task script(type: PythonTask) {
command = "path/to/script.py 1 2"
} called: 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']
} Command parsingWhen command passed as string it is manually parsed to arguments array (split by space):
To view parsed arguments run gradle with Environment variablesBy default, executed python can access system environment variables (same as To declare custom (process specific) variables: task sample(type: PythonTask) {
command = "-c \"import os;print('variables: '+os.getenv('some', 'null')+' '+os.getenv('foo', 'null'))\""
environment 'some', 1
environment 'other', 2
environment(['foo': 'bar', 'baz': 'bag'])
} Map based declaration ( System variables will be available even after declaring custom variables (of course, custom variables could override global value). NOTE: environment variable could also be declared in extension to apply for all python commands:
ConfigurationPython locationOn linux, plugin will use python {
pythonBinary = 'python'
} This will force python 2 for linux. Also, this may be handy if python binary is named differently. To use non global python: python {
pythonPath = 'path/to/python/binray/'
}
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论