2

I would like to set the value of some objects in PyCharm before running my code and debugging. It would save me a lot of time, because I would not have to enter by myself the values of those objects.

For example, I would like to define foo = 1 and bar = 2 for the following code. (In reality, it is for more complex code with more objects.)

foo = input("value of foo ?")
foo = int(foo)
bar = input("value of bar ?")
bar = int(bar)
total = foo + bar

Is there a way to do that in PyCharm without modifying the code ? I do not want to do foo = 1 and bar = 2 because I would have to modify the code. I looked online and tried to use the "Parameters" and "Environment variables" of the Run/Debug Configurations but could not succeed. Thank you for your help.

3
  • Please show what you tried with setting "Parameters" and "Environment Variables" in your run configuration. Commented Jun 15, 2018 at 16:16
  • I defined the "Environment Variables" as : "PYTHONUNBUFFERED=1;foo=1;bar=2" but when I run, I still need to enter an input. When I remove "input()" and instead just put total = foo + bar =, I have "NameError: name 'foo' is not defined". Commented Jun 15, 2018 at 16:29
  • Environment variables are not the same as the python variables. I suggest that you do some research about how to read environment variables. Commented Jun 15, 2018 at 17:34

2 Answers 2

2

Latest PyCharm 2018.2 EAP introduces input redirection feature, I believe this is exactly what you want:

enter image description here

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

Comments

0

I can think of three options:

  1. Read the values from environment variables. You can set the values of the environment variables in your run configuration. You can even create multiple run configurations for different values and combinations of values.

  2. Read the values from command line arguments. You can provide these arguments in your run configuration.

  3. Run your program from the command line and redirect input from a file or pipe input from another command.

Either way, you set the run configuration once and only modify it when you need to change the values.

Note that this changes the interface to your program. Any users will have to set the environment variable or provide the command line arguments. For many situations command line arguments are very common when writing text-based programs. Optionally, you can write code that tests if the environment variables or command line arguments exists, and if not provide a prompt for the user to type them in.

Comments

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.