您可以在BASH(也许是ZSH)和Python中激活Python虚拟环境,但不能在Java或任何其他环境中激活。要理解的主要问题是,虚拟环境的激活不会产生系统魔力——激活脚本只是改变
当前环境
,而python虚拟环境准备更改shell或python,但不更改其他内容。
就Java而言,这意味着当你调用
runCommandInDirectory()
它运行一个新的shell,新shell简单地激活虚拟的Env,但是shell退出,所有激活“虚拟EnV”的改变都消失了。
这反过来意味着,如果需要在虚拟环境中运行一些shell或python命令,则必须在
RunCommandIndirectory()
调用:
String [] commands1 = new String [] {"/bin/bash", "-c", ". /Users/simeon/..../venv2.7/bin/activate; script1.sh"};
runCommandInDirectory(path, commands)
String [] commands2 = new String [] {"/bin/bash", "-c", ". /Users/simeon/..../venv2.7/bin/activate; script2.sh"};
runCommandInDirectory(path, commands)
对于python脚本,可以运行
python
从环境中自动激活环境:
String [] commands = new String [] {"/Users/simeon/..../venv2.7/bin/python", "script.py"};
runCommandInDirectory(path, commands)