Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, e.g. execute an equivalent of myScript.py -u $myVar on Linux, or myScript.py -u %myVar% on Windows.

How do I specify that in the PyCharm configuration? I don't want my script to depend on the name myVar, just on the content of that environment variable.

like image 429
gt6989b Avatar asked May 14 '14 19:05

gt6989b


People also ask

How do I run a command-line argument in PyCharm?

Passing Command-line arguments in PyCharm If you want to pass command-line arguments to a python program, go to “Run > Edit Configurations” and set the Parameters value and save it.

How do I change the run configuration in PyCharm?

From the main menu, select Run | Edit Configurations. Alternatively, press Alt+Shift+F10 , then 0 . In the left-hand pane of the run/debug configuration dialog, click Edit configuration templates…. In the Run/Debug Configuration Templates dialog that opens, select a configuration type.


2 Answers

in PyCharm Run/Debug configuration for "Script Parameters:" Enter

-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. For that to work, you will need to look into "Before Launch" configuration

like image 98
Alok A Avatar answered Sep 24 '22 20:09

Alok A


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.

like image 31
Cassius Puodzius Avatar answered Sep 22 '22 20:09

Cassius Puodzius