gradle interactions with conda and pyenv

Issue

I have a simple gradle task which shells out to python to do some deployment stuff. However, it seems to use an arbitrary version of python in its commandLine call.

task deploy(type: Exec) {
    dependsOn 'clean'
    dependsOn 'build'
    tasks.findByName('build').mustRunAfter 'clean'
    workingDir "$buildDir/deploy/"
    commandLine "which", "python3"
}

yields /Users/cchow/projects/<project>/venv/bin/python3 as expected. I have a virtualenv active when I run the gradle command.

However, the below yields something unexpected

task deploy(type: Exec) {
    dependsOn 'clean'
    dependsOn 'build'
    tasks.findByName('build').mustRunAfter 'clean'
    workingDir "$buildDir/deploy/"
    commandLine "python3", "-m", "yaml"
}

yields /usr/local/Caskroom/miniconda/base/bin/python3: No module named yaml. I do have conda installed, but conda installs should have lower priority than virtualenvs. According to env when executed via commandLine "env":

PATH=/Users/cchow/projects/<project>/venv/bin:/usr/local/Caskroom/miniconda/base/condabin:<rest of path>

Why would it pick the virtualenv py3 for the former but the conda py3 for the latter block?

Solution

I’ve encountered this problem too and have found this as a workaround:

project.exec  {
    commandLine "sh", "-c", "python -m foo"
}

Answered By – Steve Vermeulen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published