4

I'm using PyCharm 5.04 and Python 3.4 as interpreter. I can't use input() function in my code and simply run it (Shift+F10), because PyCharm does not give me a chance to enter my input. Here is the code:

x=''
while x!='stop':
    x=input('enter x')
    print(x)
print(x)

If I run it, I'll see:

enter x
enter x
enter x
.....

However, if I highlight the code, click right mouse button and chose in context menu 'execute selection in console', everything will work fine. Also, I can add a string 'from sys import argv' at the beginning of my cod, execute it in terminal by typing 'python name_of_script.py' and everything will be working fine to (just in case, I have ubuntu 15.10).

Is something wrong with my PyCharm settings? What should I do to be able to execute code by simple running it (with Shift+F10, as I get used)? I don't have problems with any other code if it does not have input().

Any help would be appreciated.

user48115
  • 445
  • 1
  • 8
  • 18
  • Your code works fine for me in Pycharm 5.04. Shift+F10 may run a different Run/Debug configuration than what you expect (see the drop down menu at the top right of the screen). You could also try Ctrl+Shift+F10 to run the current file (in case the run config points to another file) I would recommend that you copy your code to a brand new project and test it there. If it works, it's most likely that your Run/Debug config was changed in your other project. – guiweb Mar 06 '16 at 16:17
  • Thank you, @guiweb! Ctrl+Shift+F10 did not helped, but I copied my code to a new project, run code there and that solved my issue. – user48115 Mar 06 '16 at 18:06
  • Great! I just put this in a more detailed answer to help others who might have similar issues. – guiweb Mar 06 '16 at 18:55
  • The setting you need is to disable "Open terminal afterwards" it the run options. I just encountered this. –  Oct 23 '16 at 07:22

4 Answers4

4

I'm posting this answer for a subtle variation of this issue that I encountered, where the Python console would not let me enter my input but unlike in your case, would not move on with the loop.

I solved the problem by clicking on "Edit the configurations" on the toolbar (run/debug configurations) and disabling the option "Emulate terminal in output console".

Hopefully those having the same issue will stumble upon this thread (as I did).

EDIT: In images:

Click on the dopdown displaying the name of the file you're working on

Click on "edit configurations"

Uncheck "Emulate terminal in output console"

I haven't tried that solution with a remote interpreter.

LeCodex
  • 1,636
  • 14
  • 20
  • 1
    I have the same problem as you did and posted a the question at https://stackoverflow.com/q/48532248/4635580. I can't find the option for "Emulate terminal in output console" you spoke of. Could you point me to the solution in PyCharm 2017.3 Professional? Thanks in advance. – Isaac To Feb 01 '18 at 18:17
  • I just found that "Emulate terminal in output console" doesn't exists for an project set up with a remote interpreter which is the situation I have the problem. Do you know how to resolved the problem for remote development? – Isaac To Feb 01 '18 at 18:45
  • @candleindark I updated my answer to show the process – LeCodex Mar 26 '18 at 17:34
2

Your code seems fine. I suspect that what you're seeing is caused by a Run/Debug configuration issue, which can be the source of all sorts of unexpected behaviors.

Here is what I recommend you do to identify what is wrong:

  • Run the code with CTRL-Shift-F10 to make sure you're running the current file, and not some other file in your project. If this fixes the issue, you can simply select which file you want Shift-F10 to run by selecting it in the drop down menu at the top right of the screen, near the "play" button

  • Copy your code to a brand new project, and run it from there. If this fixes the issue, it means your code is fine and that the issue is most likely a Run/Debug configuration issue.

  • If you confirmed that your code is fine with the step above but can't start from scratch, you can edit the Run/Debug configuration by selecting "Edit" in the drop down menu at the top right of the screen, near the "play" button.

guiweb
  • 965
  • 6
  • 12
1

Mine is not the usual case, and not specific to PyCharm, but in case anyone has the same issue as me:

For me, the problem was caused by me pasting a multi-line input containing some blank lines when prompted by input() within a while loop.

The solution was to adapt my use of input() to accomodate multiple line, as described here: How to get multiline input from user

davidgw
  • 11
  • 1
0
input_string = ""

stop_code = 'stop'


try:
    while input_string != stop_code:
        input_string = input('Enter a value: \n')
        print input_string
except Exception as e:
    print e

Hope this is What you needed this works for me fine...

Henin RK
  • 298
  • 1
  • 2
  • 14