Skip to content

Latest commit

 

History

History
89 lines (70 loc) · 3.89 KB

python.md

File metadata and controls

89 lines (70 loc) · 3.89 KB

com.linkedin.python

About

The com.linkedin.python plugin adds an extension called python to the project. It also configures a python project to setup a virtual env and run the tests. There are no artifacts produced by this plugin alone, you should check out other plugins to generate artifacts for the project.

python {
   docsDir = project.file('docs').path // The location of this project's Sphinx documentation directory
   testDir = project.file('test').path // The location of this project's tests directory.
   srcDir = project.file('src').path // The location of this project's source directory.
   setupCfg = project.file('src').path // The location of this project's setup.cfg file.
   pinnedFile = project.file('pinned.txt') // A file generated by the build for pip install to consume in no pygradle builds
   pythonEnvironment = [:] // Environment variables that will be passed to python commands. This will overwrite any existing env variables.
   pipConfig = [:] // A Map of Maps to build a pip configuration file in your venv
   
   details {
       virtualEnvPrompt = "(${project.name})"
       activateLink = project.file("activate") // File you can source to activate the virtual env
       // pythonVersion = '2.7' // Sets the version of python to use, will search your PATH to get the location
       // systemPythonInterpreter = file("/path/to/python2.7") // used to force an interpreter to be used to build the venv
   }
}

Usage

plugins {
    id 'com.linkedin.python', version <latest version>
}

pipConfig Examples

Sometimes you need to configure pip inside of the venv itself. The most common use cases are when you don't use pypi.org, but you use your own internal pypi server, or you need to set a proxy. Typically, you can configure this with an operating system level pip.* file as documented on the pip web site. But occasionally you need to configure this on a per-project basis. This is where this setting comes in. Take this example

python{
     pipConfig = ['global':['index-url': 'https://<login>:<password>@your.repo.com/custom/url', 'timeout': '60']]
}

This is a map of maps. The above will result in a pip.conf (or pip.ini on windows) of

[global]
index-url = https://<login>:<password>@your.repo.com/custom/url
timeout = 60

All of the standard pip configurations will work, this is simply a way of generating the file in the venv

Conditional Task Disabling

PyGradle will automatically disable tasks if certain conditions are present.

  • Product install will abort if no projectRoot/setup.py file is present
  • pytest will skip if the configured python.testDir doesn't exist
  • coverage will skip if the configured python.testDir doesn't exist
  • flake8 will skip if both python.testDir and python.srcDir doesn't exist
  • both Sphinx Documentation tasks will skip if the configured python.docsDir doesn't exist

Default and allowed Python version

This plugin enforces a set of default and allowed Python versions. For example, you can specify pythonVersion = '3' and you will get whatever the default Python 3 version is. Similarly pythonVersion = '2' gets you whatever the default Python 2 version is.

This plugin also enforces a set of allowed Python versions. If you choose a Python version that is not allowed, you will see an error messages such as:

> Python 3.2 not allowed; choose from [2.7, 3.5, 3.6, 3.7]

If you see this error message, you must adjust your pythonVersion setting to one of the allowed values.

Consumers of this extension can change the default Python 2, Python 3, and allowed versions by making a call on the PythonDetails object, either the one returned by PythonExtension.getDetails() or on any PythonDetails instance you create, e.g. in Groovy:

pythonDetails.setPythonDefaultVersions('2.7', '3.6', ['2.7', '3.5', '3.6', '3.7'])