简体   繁体   English

Python 2.7从bash读取导出的变量

[英]Python 2.7 read exported variables from bash

I have a python2.7 script which is calling a bash script, inside the bash script i'm exporting 2 variable i've tried with declare -x and with export , then i'm trying to read this variables in the python script using os.getenv and i also tried using os.environ but i'm getting a None type each time. 我有一个python2.7脚本正在调用bash脚本,在bash脚本中我正在导出2个我已经尝试过的declare -x带有declare -xexport变量,然后我试图使用以下命令在python脚本中读取此变量os.getenv和我也尝试使用os.environ但每次都得到None类型。 Here is a snippet from my scripts: 这是我的脚本的片段:

Python: 蟒蛇:

def set_perforce_workspace():
        global workspace_path
        global workspace_root_path
        script_path = "somePath"
        depot_name = "TestingTools"
        dir_to_download = "vtf"
        bash_command = "./createWorkspace.sh " + "-d " + depot_name + " -w " \
                       + PerforceUtils.WORKSPACE_NAME + " -a " + dir_to_download
        print bash_command
        print os.getcwd()
        try:
            os.chdir(script_path)
        except OSError as e:
            print "Error: {0}".format(e)
        print os.getcwd()
        return_code = call([bash_command], shell=True)
        test = os.getenv("workspace_path")
        print "this is test {0}".format(test)
        workspace_path = os.getenv("workspace_path")
        workspace_root_path = os.getenv("workspace_root_path")
        print "This is the return code {0}".format(return_code)
        print "this is the workspace path: {0}".format(workspace_path)
        print "this is the workspace root path: {0}".format(workspace_root_path)
        return return_code

Bash: 重击:

declare -x workspace_root_path="$workspaceProjPath"
export workspace_path="$workspaceProjPathLocal"

From the documentation for os.environ : os.environ的文档中:

This mapping is captured the first time the os module is imported, typically during Python startup as part of processing site.py. 首次导入os模块时(通常是在Python启动期间作为处理site.py的一部分)捕获了此映射。 Changes to the environment made after this time are not reflected in os.environ, except for changes made by modifying os.environ directly. 此时间之后对环境所做的更改不会反映在os.environ中,除非直接通过修改os.environ进行更改。

So basically when your bash script changes the environment and then goes back to your python script the environment variables aren't refreshed. 因此,基本上,当您的bash脚本更改环境然后返回到python脚本时,不会刷新环境变量。 Pretty annoying. 真烦人。 As the docs say the best solution is to add your variables to the environ list directly, like: 正如文档所说,最好的解决方案是将变量直接添加到环境列表中,例如:

os.environ['PATH'] = '/bin/'

How you accomplish this is up to you, piping is a pretty decent way to do it. 如何完成此操作取决于您,管道是一种相当不错的方法。 Albeit annoying. 尽管烦人。 Unfortunately there is no way to "refresh" the environment list. 不幸的是,没有办法“刷新”环境列表。 In a lot of situations it might be best just to drop your bash script and somehow adapt it to the python script. 在许多情况下,最好只是删除bash脚本并以某种方式使其适应python脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM