The best place to experiment with Python code is in the interactive interpreter, otherwise known as a shell. The shell is a basic Read-Eval-Print Loop (REPL). It reads a Python statement, evaluates the result of that statement, and then prints the result on the screen. Then, it loops back to read the next statement.
The Python shell is an excellent place to experiment with small code snippets. You can access it through the terminal or command line app on your machine. You can simplify your workflow with Python IDLE, which will immediately start a Python shell when you open it.
In the image above, you first declare a variable, x = 5. When you call print(x), the shell shows the correct output, which is the number 5. However, when you restart the shell and try to call print(x) again, you can see that the shell prints a traceback. This is an error message that says the variable x is not defined. The shell has forgotten about everything that came before it was restarted.
Do Check Module. If no error, restart the shell to clean theenvironment, then execute the module. Output is displayed in the Shellwindow. Note that output requires use of print or write.When execution is complete, the Shell retains focus and displays a prompt.At this point, one may interactively explore the result of execution.This is similar to executing a file with python -i file at a commandline.
Idle defaults to black on white text, but colors text with special meanings.For the shell, these are shell output, shell error, user output, anduser error. For Python code, at the shell prompt or in an editor, these arekeywords, builtin class and function names, names following class anddef, strings, and comments. For any text window, these are the cursor (whenpresent), found text (when possible), and selected text.
Upon startup with the -s option, IDLE will execute the file referenced bythe environment variables IDLESTARTUP or PYTHONSTARTUP.IDLE first checks for IDLESTARTUP; if IDLESTARTUP is present the filereferenced is run. If IDLESTARTUP is not present, IDLE checks forPYTHONSTARTUP. Files referenced by these environment variables areconvenient places to store functions that are used frequently from the IDLEshell, or for executing import statements to import common modules.
By default, IDLE runs user code in a separate OS process rather than inthe user interface process that runs the shell and editor. In the executionprocess, it replaces sys.stdin, sys.stdout, and sys.stderrwith objects that get input from and send output to the Shell window.The original values stored in sys.__stdin__, sys.__stdout__, andsys.__stderr__ are not touched, but may be None.
Most tkinter programs run root.mainloop(), which usually does notreturn until the tk app is destroyed. If the program is run withpython -i or from an IDLE editor, a >>> shell prompt does notappear until mainloop() returns, at which time there is nothing leftto interact with.
When running a tkinter program from an IDLE editor, one can comment outthe mainloop call. One then gets a shell prompt immediately and caninteract with the live application. One just has to remember tore-enable the mainloop call when running in standard Python.
IDLE contains an extension facility. Preferences for extensions can bechanged with the Extensions tab of the preferences dialog. See thebeginning of config-extensions.def in the idlelib directory for furtherinformation. The only current default extension is zzdummy, an examplealso used for testing.
I know this is a noob question but all these terms have me a bit confused. Is anyone able to explain it simply to me? I looked up what an IDE is and I thought that was the python shell? And the IDE and IDLE look similar to each other as well. Any help for a confused noob? lol Thanks in advance
I noticed several times that a simple python script (with some straightforward algebraic computation in a loop) runs considerably faster (up to a factor 6) when launched on a shell command line as compared to a run via IDLE's shell. There is nothing fancy going on in the script. I only print a loop variable to visually follow progress in the loop.
Yes, the print statement really is the main cause of slow-down within idle. The stdout and stderr streams are captured by IDLE, and a whole bunch of Tk commands are executed to render the text appropriately. Removing your output to stdout should speed things up again.
UPDATE: 2014/12/26 - It's been a few years since I wrote this article, and I've marked the issues that have been solved. Thanks, IDLE dev team!
1) PROBLEM: In the shell window, if you click anywhere but on the current line and move the cursor there, the window stops handling key strokes.
Another UI problem that being able to move the cursor back up introduces: When my students' code would raise some exception, they thought they could correct it by scrolling back up, deleting the original code, and then re-typing it out (as if the interactive shell were a word processor). They would move the cursor up, but would be foiled when couldn't change any of the existing text. Getting rid of this misleading cursor stops this issue much earlier in the process and reduces confusion.
The entire status bar is wasted screen real estate because it is only used for that tiny display of the current line and column. Also, why even bother having that status bar with line/column information for the shell window?
UPDATE (2014/9/7) - Thinking about item 1 more, to better convey that you can't type new instructions while the cursor is off the last line, I'd move the final >>> prompt to a small separate window on its own at the bottom of the interactive shell window. This way, the top area is read-only (though the cursor can be moved there to highlight text) while it is understood that instructions are only entered at the bottom when the cursor is there. Also, I've seen cases with the kids I teach where they think of the interactive shell window as a word processor, and when they make errors they try to move the cursor back up to "correct" their code, rather than just retype it in.
A lot of times, I would like to terminate a contingency analysis process (that might take more than half hour) in the middle in the python shell of IDLE, so that I can test my another script. I tried Ctrl+C and Ctrl+Z,but neither worked. I searched through all the drop-down menus of IDLE, no options seem to solve my issue.
I suggest you not to use python IDLE if possible as it is very simple and not very programming-friendly enviroment. I would recommend to use PyScripter or Notepad++, both of them are free and much better shells for writing in python. In PyScripter you can terminate running program by pressing Ctrl-Alt-F9 ('Abort debugging'). In Notepad++ for running python programs you will need to install plugin NppExec and run python scripts by pressing F6 (for the very first time you will need to enter this line for make it work: C:\Python27\python.exe "$(FULLCURRENTPATH)" ). After program starts, if you'll press F6 again "Terminate" button appears and you can kill active process. Try this and you will never ever want to come back using native python IDLE :)
I noticed an issue where if I have kept the gnome shell idle for sometime about 15 seconds to 20 seconds (sometime 5 seconds also) then if I move the mouse cursor, it sticks for 1 secs and then moves.
We have some users who start Spark shells and leave them open indefinitely. Without using dynamic resource allocation to deallocate executors - would it be possible to write something to poll YARN to determine if a Spark shell isn't doing anything, and after X time period of inactivity, kill it?
Heh, that is a large part of what dynamic allocation was meant for, so you could have a long running process that could only consume resources when it's active. and a shell sitting open is a prime example of that.
To some degree you can manage this via resource pools in YARN, and restrict a user, group or perhaps type of usage to a certain set of resources. This would be a pretty crude limit though, just a cap on the problem. Open shells would still keep resources.
If the problem is users leaving their shells open, I don't think I can trust them to add extra parameters to their CLI arugments to ensure they don't eat up extra resources (from their point of view, why would they care if they're using up my resources?).
df19127ead