How to pass an environment variable as a command line parameter in Run/Debug configuration in PyCharm?

后端 未结 3 735
梦如初夏
梦如初夏 2021-01-05 02:02

I am trying to learn PyCharm, need to pass an environment variable as a command line parameter to my process, e.g. execute an equivalent of myScript.py -u $myVar

3条回答
  •  清酒与你
    2021-01-05 02:30

    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". For notice, I'm using 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.

    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).

    Run the newly created wrapper script and have a happy debugging. Not a charming solution, but it works at least.

提交回复
热议问题