PyDev自定义解释器脚本不读取Env变量

时间:2016-08-01 21:17:39

标签: python shell environment-variables pydev

我在 InterpreterInfo 的脚本元素中定义了自定义python解释器。我还在那里定义了一些环境变量,当我在PyDev UI中查看解释器信息时,我会看到它们。

解释器可执行文件是一个shell脚本(tcsh),它包装了一个python解释器。它工作正常,除了它无法看到为解释器设置的环境变量。我认为这些将在可执行脚本的环境中设置,但它们不是。有没有办法可以将为解释器信息定义的变量传递给解释器的可执行脚本?

编辑:

当任何PyDev项目被添加到工作空间时,我修改了脚本元素,以便它用包装python解释器的shell脚本替换python解释器的路径。这是Java代码,省略了异常处理。

private static InterpreterInfo createInterpreterInfo(String interpreter, IProgressMonitor monitor) throws CoreException {
        final File script = PydevPlugin.getScriptWithinPySrc("interpreterInfo.py");
        Tuple<String, String> outTup;
        outTup = new SimplePythonRunner().runAndGetOutputWithInterpreter(interpreter, script.getCanonicalPath(), null, null, null, monitor, "UTF-8");

        String interpreterXml = outTup.o1;
        String replacedInterpreterXml  = interpreterXml;

        //substitute the script for the <executable> element in the XML string
        String executableStartTag = "<executable>";
        String executableEndTag = "</executable>";
        int executableElementStart = interpreterXml.indexOf(executableStartTag) + executableStartTag.length();  
        int executableElementEnd = interpreterXml.indexOf(executableEndTag);

        replacedInterpreterXml = interpreterXml.substring(0, executableElementStart) + interpreter + interpreterXml.substring(executableElementEnd);

        // Add an environment variable
        String xmlEndTag = "</xml>";
        int xmlEndElementStart = replacedInterpreterXml.indexOf(xmlEndTag);  

        replacedInterpreterXml = replacedInterpreterXml.substring(0, xmlEndElementStart) +
                "\n<env_var>" +
                SOME_VARIABLE + "=" + ENV_VAR_VALUE +
                "</env_var>\n" +
                replacedInterpreterXml.substring(xmlEndElementStart);

        return InterpreterInfo.fromString(replacedInterpreterXml, false);
    }

然后,包装器脚本执行一些环境设置并使用命令字符串调用python解释器。当解释器返回脚本时,环境会被拆除。该脚本无法通过查看shell环境或让python解释器查询其环境来查找InterpreterInfo上设置的环境变量。

以下是包装器shell脚本的表示:

#!/bin/tcsh

#redirect output to avoid corrupting output to `InterpreterInfo`
echo Env variable SOME_VARIABLE = $SOME_VARIABLE > /log/file #throws error because SOME_VARIABLE is not set
python -c "import os;print os.environ['SOME_VARIABLE']" > /log/file #no value set for SOME_VARIABLE

do_environment_setup

python $*

do_environment_cleanup

0 个答案:

没有答案