简体   繁体   English

从python脚本设置bash环境变量

[英]Set bash environment variable from python script

I am running 4 Python scripts which in short should set environment variables. 我正在运行4个Python脚本,简而言之,这些脚本应该设置环境变量。

Point is, using os.environ['TEST'] (say) sets it only for the scope of the Python script that is running it. 重点是,使用os.environ['TEST'] (例如)仅针对运行它的Python脚本的范围设置它。

$ python test.py arg1
$ echo $TEST    < ------------

This returns blank. 这将返回空白。 Whereas printing from within the Python script shows a value in TEST 而从Python脚本中进行打印会在TEST中显示一个值

os.environ.get('TEST')

How does one set environment variables from python script? 如何从python脚本设置环境变量?

As a workaround to put it another way, (alternate question) how does one share data between Python scripts? 作为另一种解决方法,(另一个问题)一个人如何在Python脚本之间共享数据?

You seem to have a complete misunderstanding about how processes and environments work. 您似乎对流程和环境的工作方式有完全的误解。

$ python test.py arg1
$ echo $TEST

The first line will create a new process, and it's an entirely separate process from the shell itself. 第一行将创建一个进程,它是与Shell本身完全独立的进程。 Though the values of environment variables may be inherited from the environment of the parent process, the new process has its own environment. 尽管环境变量的值可能是从父流程的环境继承的,但是新流程具有自己的环境。

By the time the second line executes, the process and environment from the first line no longer even exist! 到第二行执行时,第一行的过程和环境甚至不复存在! You can not easily modify the environment of the shell's process from within the Python process. 您无法从Python进程中轻松修改Shell进程的环境。

As a workaround to put it another way, (alternate question) how does one share data between python scripts? 作为另一种解决方法,(另一个问题)一个人如何在python脚本之间共享数据?

Not via the environment - after the Python process completes, ie when your script exits, its environment has been torn down. 不通过环境-Python进程完成后,即退出脚本时,其环境已被破坏。 The "alternate question" is perhaps too broad, since there are many ways to share data between Python processes, but I suggest you start by reading/writing the data to/from a file. “替代问题”也许太笼统了,因为有许多方法可以在Python进程之间共享数据,但是我建议您首先从文件中读取/写入数据。

It is not possible to set an environment variable for a parent (or ancestor) process environment. 不能为父(或祖先)流程环境设置环境变量。

Some bash scripts go around this requirement by executing with source (which runs them inside the current bash, instead of spawning a bash process), but this is obviously never going to work with a python script, as you can't execute a python script inside the bash process itself without spawning a python subprocess. 一些bash脚本通过执行source来解决此要求(它在当前bash中运行它们,而不是生成bash进程),但是显然这永远不会与python脚本一起使用,因为您无法执行python脚本在bash进程本身内部,而不会产生python子进程。

Some other programs (such as ssh-agent ) output the necessary assignments to the standard output in such a way that their output can be run through eval in bash; 其他一些程序(例如ssh-agent )将必需的分配输出到标准输出,以使它们的输出可以通过bash中的eval运行; this works, though it requires the caller's cooperation (ie running eval $(ssh-agent) instead of just ssh-agent ). 尽管需要调用者的配合(例如,运行eval $(ssh-agent)而不只是ssh-agent ),但此方法有效。

About your sub-question - There are many ways to share data between processes: command-line arguments, pipes, named pipes, files, sockets, signals, databases (both SQL and no-SQL), other services (web and otherwise)... You would need to specify your scenario in more detail if you ask us to pick the most appropriate one. 关于您的子问题-有多种在进程之间共享数据的方法:命令行参数,管道,命名管道,文件,套接字,信号,数据库(SQL和no-SQL),其他服务(Web或其他)。 ..如果您要求我们选择最合适的方案,则需要详细说明您的方案。 But that should really be a new question, if it comes to that. 但这真的是一个新问题。

The environment block is an area of memory unique to each process. 环境块是每个进程唯一的内存区域。 The contents are determined by the way the process is started. 内容由启动过程的方式确定。 By default the parent process's environment block is copied to a child process. 默认情况下,父进程的环境块被复制到子进程。 The child is free to alter its own environment block but cannot alter it's parent's environment. 孩子可以自由更改自己的环境块,但不能更改其父母的环境。

The bottom line is that environment values are only passed in one direction, from parent to child. 最重要的是,环境值只能沿一个方向传递,即从父级到子级。

This applies to all processes, regardless of which language the programs they use are written in. 这适用于所有进程,无论使用何种语言编写程序。

"using os.environ['TEST'] (say) sets it only for the scope of the Python script that is running it." “使用os.environ ['TEST'](例如)仅针对运行它的Python脚本的范围设置它。” - correct -正确

"How does one set environment variables from python script?" “如何从python脚本中设置环境变量?” - in theory a debugger, which would probably have to be written in C, could be attached to a process and alter the block of memory. -理论上,可能必须用C编写的调试器可以附加到进程并更改内存块。 That is difficult, dangerous (you could overwrite the wrong memory), a huge security risk, and a maintenance nightmare. 这是困难的,危险的(可能会覆盖错误的内存),巨大的安全风险和维护噩梦。 You really don't want to go there. 你真的不想去那里。

Sharing data between processes is a wide question, it doesn't really matter which language you are using - the principles are the same. 在流程之间共享数据是一个广泛的问题,使用哪种语言并不重要-原理是相同的。

It depends on the type of data (binary or text), the volume of data (a few bytes or megabytes), if the data is passed in both directions (synchronisation might be needed), if the data has to be persistent (a file or database might be required), and probably a million other things I have not thought of. 它取决于数据的类型(二进制或文本),数据量(几个字节或兆字节),数据是否双向传输(可能需要同步),数据是否必须持久(文件)。或可能需要数据库),可能还有一百万我没有想到的其他事情。

You need to be more specific about which data is shared under what circumstances, including who does the writing and who does the reading. 您需要更具体地说明在什么情况下共享哪些数据,包括由谁进行写作和由谁进行阅读。

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

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