[英]How to pass an environment variable as a command line parameter in Run/Debug configuration in PyCharm?
I am trying to learn PyCharm, need to pass an environment variable as a command line parameter to my process, eg execute an equivalent of myScript.py -u $myVar
on Linux, or myScript.py -u %myVar%
on Windows. 我正在尝试学习PyCharm,需要将环境变量作为命令行参数传递给我的进程,例如,在Linux上执行
myScript.py -u $myVar
等效,在Windows上执行myScript.py -u %myVar%
。
How do I specify that in the PyCharm configuration? 如何在PyCharm配置中指定? I don't want my script to depend on the name
myVar
, just on the content of that environment variable. 我不希望我的脚本依赖于名称
myVar
,而仅依赖于该环境变量的内容。
thank you very much 非常感谢你
in PyCharm Run/Debug configuration for "Script Parameters:" Enter 在“脚本参数:”的PyCharm运行/调试配置中输入
-u ${myVar}
Note: This will work only for existing env. 注意:这仅适用于现有的环境。 variables but not for env.
变量,但不包含环境变量。 variables that you set up in the PyCharm Run/Debug configuration.
您在PyCharm运行/调试配置中设置的变量。 For that to work, you will need to look into "Before Launch" configuration
为此,您需要查看“启动前”配置
I wasn't able to define new env vars for passing them to Run/Debug configuration (as suggested by @alok-a), even if defining them on a script executed in "Before Launch". 我无法定义新的环境变量,以将其传递给运行/调试配置(如@ alok-a所建议),即使在“启动前”执行的脚本中定义了它们也是如此。 For notice, I'm using PyCharm 2018.3.4.
请注意,我正在使用PyCharm 2018.3.4。
The workaround that works for me is to create a python script that prepare the full command line and calls it using the subprocess module. 对我有用的解决方法是创建一个Python脚本,该脚本准备完整的命令行并使用subprocess模块对其进行调用。
import subprocess
# Build params line
cmd = ["python", script_path] + params.split()
subprocess.run(cmd)
Set your breakpoints in the target script (the one indicated by script_path). 在目标脚本(由script_path指示的脚本)中设置断点。
Run the newly created wrapper script and have a happy debugging. 运行新创建的包装器脚本并进行调试。 Not a charming solution, but it works at least.
这不是一个吸引人的解决方案,但至少有效。
Go to Edit configurations > Environment variables. 转到编辑配置>环境变量。 Add or edit.
添加或编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.