【问题标题】:How to use a environment variable如何使用环境变量
【发布时间】:2019-12-30 16:18:57
【问题描述】:

在终端中,我设置 www 站点和路径来为我的测试创建报告。

我设置: MY_URL=mysite:myport REPORTPA=./mypath/mysecondfolder/ pytest -s testFile.py --html=REPORTPA/report.html

我应该保存report.html,我尝试这样的解决方案:

$REPORTPA/report.html or %REPORTPA/report.html

而且效果不好。如何使用我的变量?

【问题讨论】:

标签: python


【解决方案1】:

使用os.environ(docs),像这样:

>>> import os
>>> reportpa = os.environ['REPORTPA']
>>> print(reportpa)
./mypath/mysecondfolder/

【讨论】:

  • 如果 OP 只执行 VAR=value 可能不起作用,但使用 export VAR=value 它应该可以在 Python 中访问。
  • 只要 x=y 在命令之前并且在同一行(就像在 OP 的帖子中一样),就不需要导出。试试看:REPORTPA=./mypath/mysecondfolder/ python
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 2016-02-04
  • 2016-11-05
相关资源
最近更新 更多