Setting up IntelliJ for {py}gradle

Fedor Korotkov
3 min readJul 13, 2017

There is an awesome {py}gradle plugin from LinkedIn to build, test and distribute Python projects with Gradle. It’s working pretty good but there is one problem: it’s not configuring your IntelliJ project for you! No completion, no inspections, nothing!

This quick tutorial will help to configure Python SDK for your {py}gradle project. Unfortunately I didn’t find fully automatic way to configure a Python SDK from within Gradle build scripts because all customization available at the moment are only for JDKs. But this is the next best thing!

First let’s start with an example project. We can choose one of the official example project: example-project. When you first import it in IntelliJ you can see something like this(don’t forget to install Python plugin 😉):

Red code! ☹️ Let’s fix it!

We need to create a Python SDK. {py}gradle creates a virtualenv in build folder so let’s add it to IntelliJ.

First run ./gradlew build so {py}gradle will create a virtualenv for your project.

Go to Project Structure of you project then click SDKs and add a Python SDK. Please choose Local Python SDK.

Choose pygradle/examples/example-project/build/venv/bin/python . And let’s name this SDK {py}gradle example-project SDK. SDKs are shared across all your IntelliJ projects so let’s include project name in the SDK name.

Now you can specify this SDK for your module:

This module SDK setting will survive Gradle project refreshes. Engineers will need to configure it only once.

The only thing left is to configure Python source and test directories in yourbuild.gradle file:

apply plugin: "java-library" // for source roots
apply plugin: "idea"

idea {
module {
sourceDirs += file('src')
testSourceDirs += file('test')
}
}

That’s all! Hit refresh and enjoy intelligent code assistant in your python code!

Enjoy!

--

--