简体   繁体   English

如何在PyCharm的运行/调试配置中将环境变量作为命令行参数传递?

[英]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.

相关问题 如何为 PyCharm 中的“Shell 脚本”运行配置指定环境变量? - How to specify environment variable for a "Shell Script" run configuration in PyCharm? PyCharm - 如何创建一个运行/调试配置来执行一个使用 Python 脚本作为参数的 .bat 文件? - PyCharm - How to create a Run/Debug Configuration that executes a .bat file which uses a Python script as a parameter? 如何在运行配置中传递多个 pytest 命令行选项 - How to pass multiple pytest command line options in the run configuration 如何在命令行中将环境变量传递给pytest以测试功能 - How to pass an environment variable in command line to pytest to test a function 如何在多部分命令行语句上以调试模式运行Pycharm - how to run Pycharm in debug mode on a multi-part command-line statement 从命令行传递环境变量时如何运行PyCharm项目 - How to run a PyCharm project while passing environment variables from command line 如何在Pycharm中使用命令行模拟运行按钮 - How to simulate the run button with a command line in Pycharm 在命令行传递 arguments 时如何在 pycharm 中调试? - How to debug in pycharm when passing arguments on the command line? PyCharm运行/调试配置弹出窗口静音 - PyCharm run/debug configuration popup silence Pycharm 运行/调试配置保存为默认值 - Pycharm Run/Debug Configuration saved as default
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM