简体   繁体   English

在执行python脚本时在bash中使用环境变量?

[英]Using environmental variables in bash while executing python script?

I exported variable in local environment, and need to pass that variable into bash script, which in turn will need to run python script using that env. 我在本地环境中导出了变量,并且需要将该变量传递到bash脚本中,而bash脚本又需要使用该env运行python脚本。

#!/usr/bin/env bash
export API_TOKEN=836176e9b6ce
sudo python script.py --pretty

Right now I am getting following: 现在我正在关注:

Could not find values for Program api_token.
They must be specified via either ini file, command line argument (--api-token),
or environment variables (API_TOKEN)

Is it possible to achieve? 有可能实现吗?

The environment you set in the script is not passed through to the command executed by sudo . 您在脚本中设置的环境不会传递给sudo执行的sudo To preserve environment variables, use the -E flag: 要保留环境变量,请使用-E标志:

#!/usr/bin/env bash
export API_TOKEN=836176e9b6ce
sudo -E python script.py --pretty

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

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