0

How do you debug the code in the PyCharm IDE when it requires console input? For example, I have a piece of code,

        # if the config  already exists prompt what to do
        if pc and not self.prompt.ask_yesno('project_ovverride'):
            self.prompt.say('setup_abort')
            return

enter image description here

This breaks in the line highlighted and I wasn't able to proceed for not being able to provide the console input. At the moment, I comment it out, but, there might be a way to provide the required console input as well.

Thank you.

3 Answers 3

2

If you're simply looking to input via CLI while debugging; you could simply use step into as shown below. - Enable 'Run with console' under your run configuration first.

  1. Change tab to console in your debugger.
  2. Click on step into until you see the question in the console.
  3. Input your answer as needed.
  4. Click on continue or any other action from your debugger as needs be.

If you'd like to debug through running a script in CLI you're looking for something on the lines of pdb (Python Debugger). You can read more here.

Example:

my_example.py

try:
    pdb_test = 1 / 0
except ZeroDivisionError:
    print('Argh stop it!')

Command Line:

(venv) $ python3 -m pdb my_example.py
> /my_example.py(1)<module>()
-> try:
(Pdb) s
> /my_example.py(2)<module>()
-> a = 1 / 0
(Pdb) s
ZeroDivisionError: division by zero
> /my_example.py(2)<module>()
-> a = 1 / 0
(Pdb) 

What the above is showing is merely me using s to command the pdb to step - in the documentation you can find all the commands you might want to use including continue et cetera.

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

1 Comment

Your answer helps me. Thank you.
1

Initially, we need to set the Run with Python console in the Run configuration of the PyCharm IDE and then, we can change the debugger window to the console window at the time of debugging the software. I provided the screenshots that illustrate the formula,

enter image description here

Now, switch from the debugger to the console and provide the desired input.

enter image description here

Comments

0

If you're using Pycharm 2018.3 or above you can redirect your input to a file. PS. I haven't tried this but it should work fine. Set your input file here

2 Comments

Your answer helps me. Thank you.
@Arefe use this if you are expecting multiple iterations of debugging and fixing. It really saves time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.