python - 如何从 Gradle 激活虚拟环境?

标签 python macos gradle virtualenv pybuilder

我的 gradle.build 文件中有以下 exec 命令。

exec {
        workingDir System.getProperty("user.dir")
        commandLine 'python3.6', 'buildscript.py'
    }
exec {
    workingDir System.getProperty("user.dir")
    commandLine 'python3.6', '-m', 'virtualenv', 'env'
}


exec {
    workingDir System.getProperty("user.dir")
    commandLine 'source', 'env/bin/activate'
}

exec {
    workingDir System.getProperty("user.dir")
    commandLine 'pip3.6', 'install', 'pybuilder'
}

exec {
    workingDir System.getProperty("user.dir")
    commandLine 'pyb', '-E', 'env', '-X'
}

这些都在执行 gradle build 时运行的构建任务中。 理论上,这应该运行我创建的一个脚本,该脚本创建构建我的 python 程序所需的所有文件,然后它应该创建一个虚拟环境,激活它,在其中安装 pybuilder,然后运行 ​​pybuilder。但是,命令:

exec {
        workingDir System.getProperty("user.dir")
        commandLine 'source', 'env/bin/activate'
    }

似乎失败了。它声称该目录/文件不存在,尽管它通过命令行工作。我不知道为什么会这样。这样做的全部目的是强制 Pybuilder 将我的程序依赖项安装到我创建的虚拟环境中。 pyb -E env 从技术上讲应该为我激活虚拟环境,但无论出于何种原因,它都没有将我的依赖项安装到该虚拟环境。在我们的 Jenkins 节点上,这是一个问题,因为我们不希望它们全局安装,更不用说,我无论如何都没有 root 用户权限。

任何帮助将不胜感激。如果您知道让 Pybuilder 正常工作的另一种方法,那就同样好。

最佳答案

activate 上调用 source 只是执行 shell 魔法来连接变量。您不需要总是调用 activate (在这种情况下可能不能)。相反,您应该在 pip3.6pyb 命令前加上 env/bin 来直接调用二进制文件。因此,这将是

exec {
   workingDir System.getProperty("user.dir")
   commandLine 'env/bin/pip3.6', 'install', 'pybuilder'
}

exec {
   workingDir System.getProperty("user.dir")
   commandLine 'env/bin/pyb', '-E', 'env', '-X'
}

关于python - 如何从 Gradle 激活虚拟环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49013275/

相关文章:

python - sudo python 错误版本

python - 属性错误 : 'module' object has no attribute 'ORB'

c++ - Mac 上的 App Bundle 是什么?

kotlin - Gradle 强制为您带来错误版本的 Kotlin 依赖项

spring - 线程 “main”中的异常java.lang.NoSuchMethodError:org.springframework.core.ResolvableType

java - Gradle with Eclipse - 资源未被过滤

python - 从文档中了解 PyTorch 伯努利分布

python - 为什么插入排序和快速排序相结合会得到更糟糕的结果?

macos - 在 Mac OS X 上以编程方式隐藏扩展坞

xcode - 依赖性分析警告 - XCODE 5