Windows pycharm的程序参数中未评估的环境变量 [英] environment variables not evaluated in program parameters in windows pycharm

查看:54
本文介绍了Windows pycharm的程序参数中未评估的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从数据流快速入门中运行wordcount.py示例

当我设置环境变量并在终端中使用相同的参数运行脚本时,它就可以正常工作:

  python wordcount.py-输入gs://dataflow-samples/shakespeare/kinglear.txt-输出gs://%BUCKET%/wordcount/outputs --runner DataflowRunner --project%PROJECT%--temp_location gs://%BUCKET%/tmp/ 

我尝试了以下几种解析环境变量的方法-无效:$ {ENV},$ ENV $,%ENV%.

我正在使用Windows 10 PyCharm 2019.3.1版本.

解决方案

将环境变量作为参数值传递时(例如,-project -temp_location )进行Pycharm配置,它将这些变量作为"字符串"值,而不是您在第一个屏幕截图"用户环境变量"中设置的实际值.我在StackOverflow上快速搜索了许多相关的线程,但没有找到解决方案,所以我想出了自己的解决方案,即使用您当前的设置并在解析代码中的参数之后替换值:

 #此处解析Args如果args.project =="$ {PROJECT}":args.project = os.environ.get("PROJECT")args.temp_location = args.temp_location.replace("$ {BUCKET}",os.environ.get("BUCKET")) 

但是,我认为您应该考虑何时使用它

  • 您使用PyCharm的频率要比终端机高.
  • 有很多参数引用环境变量,您必须为不同的设置更改它们的值.

希望Pycharm即将支持此功能.

Trying to run the wordcount.py example from the data-flow quickstart example via pycharm, and I ran into an issue when parsing the command line arguments that contain environment variables.

When I set the environment variables and run the script in the terminal with the same paramaters it works just fine:

python wordcount.py --input gs://dataflow-samples/shakespeare/kinglear.txt --output gs://%BUCKET%/wordcount/outputs --runner DataflowRunner --project %PROJECT% --temp_location gs://%BUCKET%/tmp/

I tried the following variations of parsing the environment variables - none worked: ${ENV}, $ENV$, %ENV%.

I am working on Windows 10, PyCharm version 2019.3.1.

解决方案

When you pass environment variables as values for the parameters (e.g., --project, --temp_location) to Pycharm configuration, it takes those variables as "string" values instead of their real values which you set on the first screenshot "User environment variables". I did a quick search for many related threads on StackOverflow but not find a solution so I came up with my own one, that is, using your current settings and replacing the values after parsing arguments in your code:

# Args parsing here

if args.project == "${PROJECT}":
    args.project = os.environ.get("PROJECT")
    args.temp_location = args.temp_location.replace("${BUCKET}", os.environ.get("BUCKET"))

However, I think you should consider using it when

  • you work with PyCharm more frequently than Terminal.
  • there are many arguments referring to environment variables and you have to change their values for different settings.

Hope Pycharm will support this feature soon.

这篇关于Windows pycharm的程序参数中未评估的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆