11

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.

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

1 Comment

I think Before Launch is being run in a separate process so it's of no use as far as setting environment variables is concerned.
1

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.

Comments

-4

Go to Edit configurations > Environment variables. Add or edit.enter image description here

3 Comments

Please read the question carefully. I was not interested in setting an evironment variable for the duration of the run. Rather, I need to pass its value as a run time parameter, as shown in the question. How does adding it to that list help?
I also would like this question answered. It'd be extremely helpful when trying to debug Horizon tests.
@woodm1979 see another answer here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.