将Python变量设置为Linux环境变量。

huangapple go评论71阅读模式
英文:

Set python variable as env variable in linux

问题

I have a variable TOTAL_PROCESSED that is defined in a Python script. 我有一个在Python脚本中定义的变量TOTAL_PROCESSED。
I want to set this variable from Python code as an environment variable in WSL. 我想从Python代码中将这个变量设置为WSL中的环境变量。
So that after the Python script execution, I can echo $TOTAL_PROCESSED and see the value passed from Python. 这样,在Python脚本执行后,我可以echo $TOTAL_PROCESSED并查看从Python传递的值。

I found an os command: 我找到了一个os命令:

Set environment variables

os.environ['TOTAL_PROCESSED'] = 'some-value' or os.environ.setdefault('TOTAL_PROCESSED', 'some-value').

However, after script execution, I wasn't able to echo this variable. 但是,在脚本执行后,我无法echo这个变量。

Can someone help me to set the value from a Python script as an environment variable in Linux? 有人可以帮助我在Linux中从Python脚本中设置这个值作为环境变量吗?

英文:

I have a variable TOTAL_PROCESSED that defined in python script. I want to set this variable from python code as environment variable in WSL. So that after python script execution I was able to echo $TOTAL_PROCESSED and see the value passed from python.

I found an os command:

Set environment variables

os.environ['TOTAL_PROCESSED'] = 'some-value' or os.environ.setdefault('TOTAL_PROCESSED', 'some-value').

However, after script execution I wasn`t able to echo this variable.

Can someone help me to set value from python script as env variable in Linux?

答案1

得分: 2

环境变量通常由其父进程传递给子进程,而不是相反。

为什么不起作用: 当启动Python脚本时,它继承自父进程(即shell)的环境,然后修改此环境,当脚本结束时,其环境将与Python进程一起被销毁。因此,您修改了子环境而不是您shell的环境。

解决方法: 尝试另一种传递数据的方式,比如写入文件或从脚本返回输出。

举例:

script.py

TOTAL_PROCESSED = 0

# ...
# 处理部分
# ...
# ( TOTAL_PROCESSED = 50 )

with open("totalprocessed", "w+") as f:
    f.write(str(TOTAL_PROCESSED))

shell

python script.py ; export TOTAL_PROCESSED=$(cat totalprocessed)

编辑: 如果您确实需要更改父环境,请访问这里: https://unix.stackexchange.com/a/38212

英文:

Environment variables are usually given to child process from its parent and not the other way around.

Why is it not working : When you start your python script it inherits its own environment from the parent process ( the shell ), then you modify this environment and when the script ends its environment is destroyed along with the python process. So you modified the child environment not your shell's environment.

Workaround : Try another way of passing data, writing to a file or returning output from the script maybe.

To illustrate :

script.py

TOTAL_PROCESSED = 0

# ...
# Processing part
# ...
# ( TOTAL_PROCESSED = 50 )

with open("totalprocessed", "w+") as f:
    f.write(str(TOTAL_PROCESSED))

shell

python script.py ; export TOTAL_PROCESSED=$(cat totalprocessed)

EDIT: If you really need to change parent environment head here: https://unix.stackexchange.com/a/38212

huangapple
  • 本文由 发表于 2023年5月17日 17:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定