How to give jupyter cell standard input in python?
Asked Answered
T

9

66

I am trying to run a program on a jupyter notebook that accepts user input, and I cannot figure out how to get it to read standard input. For example, if I run the code with shift-enter:

a = input()
print(a)

the cell indicates it is running, but does not accept input from me. How do I get it to accept input?

Thoer answered 23/1, 2016 at 19:54 Comment(5)
What browser are you using?Attorn
On chrome with jupyter notebook version 4.0.6 this works fine.Chubb
I am using Chrome. What happens for you when you hit shift-enter?Thoer
@AlexanderWhatley a small input box appears under the current cell. I can there enter my input.Chubb
Ok, I restarted my computer, and everything worked fine. I have no clue what happened the first time....Thoer
Z
91

Use the raw_input() (for Python 2) or input() (for Python 3) methods.

Example code:

a = raw_input()
print(a)

Example notebook:

Zymo answered 3/6, 2017 at 20:25 Comment(1)
They are already doing that, so what was the problem?Ulyssesumayyad
U
7

I came across the same problem, using the input in jupyternotebook, it blocks the execution and it does not work until restarting the program, so I added a print () after each input and my program is working.

Unorganized answered 5/3, 2019 at 21:0 Comment(0)
S
3

Probably you hit Shift-Enter a second time without completing the first input with Enter, so the kernel was always waiting until the first command completed, before executing it again. If you use in the menu

"Kernel", "Interrupt",

all active commands are stopped (including the second execution of the box) and the problem should be solved without restarting the computer (or the browser / the kernel).

Sevenfold answered 24/5, 2016 at 8:48 Comment(0)
A
2

You are doing it right, you ony have to restart the kernel (over the Run button)

Adalbertoadalheid answered 3/2, 2020 at 9:33 Comment(1)
So every time they want to take user input they have to restart the kernel? That doesn't sound right to me.Ulyssesumayyad
P
2

Update, it works as input() I noticed this thread is old, but still shows up in search results. So, here is the right way. You can use input() in the ipynb notebooks.

Polygon answered 19/4, 2023 at 19:35 Comment(0)
L
1

This might sound stupid, but it may not be obvious for someone: if you are using Visual Studio Code, the input must be typed in the bar that pops up at the top of the screen (and not right below cell). enter image description here

Longing answered 1/7 at 9:13 Comment(0)
M
0

use raw_input instead of input if you are using python 2 version. if u still getting same problem then,

click on kernel then "restart and run all" and try to run the code again. this will fix it.

Mod answered 9/10, 2017 at 8:16 Comment(0)
A
0

This worked for me:

print(input('Write anything:'))
Aladdin answered 14/11, 2023 at 13:29 Comment(0)
Q
-1

Had this problem in 2024. Nothing worked. Turns out the first line in the notebook was my input() statement. Nothing worked as long as that was the first line, but after some random statements, input() just started working. So... The details are a little thin, but try a simple print() statement first - see if that gets things going in your code.

Quickstep answered 10/6 at 20:10 Comment(6)
Please check that your solution has not already been provided before posting an answer. Thanks.Hardnett
James, what type of kernel were you using? Or where were you working from? JupyterLite / JupyterLab with the Pyodide-based kernel requires some special handling at this time. Even in a normal kernel, input() will cause things to hang as it waits for the input. (See here for example of a full report of that.)Shizukoshizuoka
@LTyrone, I did. There were variants that caught my eye... but nobody said what I said. Can you point to the solution?Quickstep
@Wayne, although it's a little weird, I did figure out how input() works (as stated) - running regular Jupyter Notebook with Python3 ipykernel locally on Windows under pip (not Conda)Quickstep
With this thread dating back to 2016, putting that JupyterLite/pyodide-based kernel would be different is probably good to have somewhere. I think L Tyrone was pointing out the the answer by Andrea Longarini pretty much already covers what you say here.Shizukoshizuoka
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Nellenelli

© 2022 - 2024 — McMap. All rights reserved.