How do I make a python shell start running - python

I wrote a program in my editor and clicked run(F5). Afterwards, in the python shell, I keyed in the desired inputs. What should i do next to make it start running? Attached picture is python editor and python shell.
Thanks!

Use CTRL-D to stop entering new sys.stdin.read() input. Also don't use this.
https://teamtreehouse.com/community/whats-the-difference-between-sysstdinread-and-input

Related

Python Idle help pager

From the Python Idle shell, the output from the help() command is not paged. Is there any way to achieve this?
As a workaround I've tried to use pydoc.pager() but with no success. The following works in a normal Python shell started from the terminal but not in the Python Idle shell.
import pydoc
def ihelp(thing):
'''Render text documentation, given an object or a path to an object, and
sends the resulting text to a pager.'''
pydoc.pager(pydoc.render_doc(thing))
Edit: Just to avoid misunderstandings. The ihelp() functions gets the help text both in a terminal Python shell and in the IDLE shell. Paging only works in the terminal shell.
You code works in delivering help output but not in paging it. The pager executes in the user code run process and does not know how to interact with an IDLE shell in a separate gui process. I don't believe you can change this.
To address this issue, IDLE has a new feature, starting with 3.6.7 and 3.7.1 (the release candidates are available now, the final releases will be out in a week or so). IDLE squeezes 'long' output to a button. (By default, 'long' is 50 lines, but user can modify this. Shorted output can be squeezed by right-clicking on it.)
>>> help(int)
[ Squeezed text (241 lines) ] # <== tkinter button
>>>
You can expand the text in place, with a double click, or into the clipboard or a separate non-modal view window, with a right click. A separate window lets one scroll or page up and down in the help entry while writing a statement in the Shell.

getch() in Python displaying wrong output

I tried running this code in python
from msvcrt import getch
while True:
char = getch()
print char
but this is displaying the character 'ΓΏ' infinitely.
Can anyone help me with this
Thanks in advance
I don't blame you for wanting to use IDLE for debugging but PDB, while not as convenient, can do anything that IDLE can do and it doesn't have this problem (at least in Windows XP-W10). Under PDB both kbhit and getche work correctly. Whether debugging or not, in W10 your script's first use of getch returns junk before the user presses a key but subsequent use is clean. This isn't necessary in earlier versions of Windows but it is always a good idea to precede your real use with if kbhit() : getch() to ensure a clean buffer before asking for user input.
It just works fine when I run the same program via command line.
I believe you are trying in an IDE? If you are using pycharm, the same query has been posted to them and it is unanswered for like a year.
Link : Using getch() in pycharm
Pls try the same code in command prompt and let us know.
Updated answer :
getch() requires a working "console" window, and when you run Idle you don't get one as its set to use pythonw.exe. Thus, it's technically not able to process any keystrokes and returns immediately. You could try to verify that by starting Idle from the command line with the standard python. (But you may have to be in that console window to trigger the getch() to return)
I personally verified your program by launching IDLE from command line. Well it does not return that character indefinitely atleast. It doesnt return anything for that matter.
Just out of curiosity , do you really need to use IDLE for this program? Wouldn't the regular command line suffice? If yes, am sorry I couldnt be of much help for this question.
To launch idle from command prompt : python C:\Python27\Lib\idlelib\idle.py
Source for the answer : IDLE does not return getch() characters

How to give jupyter cell standard input in python?

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?
Use the raw_input() (for Python 2) or input() (for Python 3) methods.
Example code:
a = raw_input()
print(a)
Example notebook:
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.
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).
You are doing it right, you ony have to restart the kernel (over the Run button)
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.

Python shell issue when using Data Nitro

I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).

How to initiate python program with a change in the input?

I have an application made in python, this application takes input from a separate text file called input.txt, i have to design the application in such a way that application runs automatically with any input to input.txt. Such that i need not go to command prompt and run the program, if i give the input, or change the text present in input.txt, python program should start execute automatically.
Please help me out with this :)
Thank you :) :)
You can run your program once and watch the filesystem changes for new or modified file. Try using python-inotify for linux:
https://github.com/seb-m/pyinotify

Categories

Resources