How to Run Python without a Terminal - python

I want to teach some students basic Python programming without having to teach them how to use the terminal. Recently, I was teaching a 2 hour intro session, and not only did teaching terminal stuff take a long time, but it also intimidated a lot of the students. I would like a solution where students wouldn't have to leave graphical user interfaces that they were comfortable with.
I also want the solution to let them execute a particular Python file (eg, not just using the interactive Python interpreter) and see the output from printing things.
Thanks!

Surely that's what IDLE is for? It's not much good as an IDE, but it does work well for exactly what you describe - opening modules and executing them, and running commands in an interactive shell.

How about this?
https://www.wakari.io/
All in the browser.
Bonus points, you introduce them to IPython as well.
Take a look at the gallery too, https://www.wakari.io/gallery

You might want to try something like this: http://repl.it/languages/python

Perhaps you want something like this:
http://learn.adafruit.com/webide/overview

When I'm not near my own PC, I use ideone.com. I like that it is a universal IDE, which for me means both C++ and Python.

Related

How to merge pythontutor.com with atom.io?

I am pretty new into programing and Atom...Though i have some gut feeling that pythontutor.com could be easily integrated to work with Atom, since Atom is built on web technologies and pythontutor.com allows embeding of both step by step and the live modes which are pretty awesome. I have this imagined like split screen in atom. As you write, the code updates on the pythontutor.
I tried doing this myself.. but when i looked into the coffee scripts and stuff it was just scary! I only know little bit of Python, and i'd like to continue lerning it with Atom :)
Thank you!
After reading some PythonTutor documentation it seems that you can only embed static visualizations (i.e. you run your Python code once and it records the trace). I don't believe you can embed an interactive PythonTutor session.
If you want to stick with Atom, I might suggest the python-debugger package. It's not as learner-friendly as PythonTutor, but it's got some nice interactive debugging features.

What's the closest thing to SLIME for python ? / What's the best way to use python from EMACS?

I'm trying to write some python, and I'm used to the lispy way of doing things, a REPL in EMACS and the ability to send arbitrary code snippets to the REPL. I like this way of developing code, and python's built-in IDLE seems to do it pretty well. However I do like EMACS as an editor.
What's the best thing analogous to SLIME for Python?
So far:
It seems that the trick is to open a python file, and then to use 'Start Interpreter' from the Python menu, after which you get an Inferior Python buffer. You can then use C-c C-c to load the whole buffer you're editing into the 'REPL', and use normal copy and paste to put snippets into the REPL.
This works as far as it goes. Is there any way to say 'reevaluate the big thing that the cursor is in now and display the answer', or 'reevaluate the thing the cursor is just at the end of and display the answer', like M-C-x and C-x-e in SLIME?
And it all seems to work better if you use the python-mode.el from Bozhidar's answer
There is ipython.el which you can use for some extended functionality over the vanilla python-mode. Ropemacs provides a few extra completion and refactoring related options that might help you. This is discussed here. However, I don't expect you're going to get anything close to SLIME.
I think the new python.el is a much better idea. It's under active development, it can spawn a python shell and send function definitions, buffers and files to it. It also has better than average re-indent support. It's rumoured that in Emacs 24 it might become the default python mode in Emacs.
python-mode is the default mode for editing Python in emacs. It includes commands for evaluating the buffer and running an inferior interpreter instance.

Lightweight console/IDE for Python?

I often use ipython (or the regular python shell) to test python code snippets while coding, and it's been very useful. One shortcoming of this, though, is that if I want to test a multi-line segment of code, or want to write multiple lines of code before running, it isn't very convenient to have to do it "line by line". And even going back to change some of the lines is cumbersome because you have to re-type all the code that comes after it.
I'm playing with Groovy right now and I find that it has an excellent solution to this problem: the Groovy Console. You just write all the code you want, it's just like a regular editor; and then you hit run Ctrl+R (Cmd+R actually since I'm on a Mac) and it runs everything at once. If you want to change something (e.g. if there are errors), then that's easy too -- just change it and Ctrl+R again.
Is there an equivalent of this available for python? Or do you have any recommendations on a way to achieve similar behavior? I could just create a new file, save it, and then python <filename>.py from the shell. But that's just too many steps and would be cumbersome. Eclipse may be an option, but it's too heavyweight. I'm really looking for something lightweight that I can just spin up when I want to test something and then get rid of it just as quickly.
I'd be interested to hear any ideas/suggestions!
Thanks
You might give DreamPie a try. As far as I can tell from a quick read of the groovyConsole page you linked to, DreamPie features a similar input area/output area division (they call it "code box" and "history box"). The code you execute is by default cleared from the code box - which groovyConsole apparently doesn't do - but you can easily retrieve it (Ctrl+Up), or change a preference setting to "Leave code in the code box after execution".
Have you tried using IDLE, the standard Python IDE? You'd have to save the code as <filename>.py within IDLE, but after that you can run it using F5.
The Python docs link to this intro to IDLE, which may be helpful even if it's a little outdated.
I am using emacs and its python-mode.
C-c C-c: evals the current buffer
but you can also eval region (ie selection), functions etc ...
You can even make python-mode use ipython (like I do).
See http://ipython.scipy.org/dist/ipython.el . It works nicely
Did you try PyCrust? It has excellent multi-line editing, copy/paste support.
PyCrust can be found in wxPython Docs and Demos.

How do I make a command line text editor?

I have gotten to know my way around a few programming languages, and I'd like to try my hand at making a command-line text editor -- something that runs in the terminal, like vim/emacs/nano, but is pure text (no guis, please). Preferably, I'd like to do this in python. Where do I start? Are there any (python) libraries to do command-line applications?
try python curses module , it is a command-line graphic operation library.
Take a look at Curses Programming in Python and this as well.
Another option if you want to write a TUI (Text User Interface) without having to descend to curses is Snack, which comes with Newt.
Kids today! Sheesh! When I was starting out, curses was not in widespread use!
My first text editors worked on actual mechanical Teletype devices with actual paper (not a philosophical "TTY" device with a scrolling screen!)
This still works nicely as a way to edit.
Use the cmd module to implement a bunch of commands. Use the 'ex' man page for hints as to what you need. Do not read about the vi commands; avoid reading about vim.
Look at older man pages for just the "EX COMMANDS" section. For example, here: http://www.manpagez.com/man/1/ex/.
Implement the append, add, change, delete, global, insert, join, list, move, print, quit, substitute and write commands and you'll be happy.
Curses type libraries and resources will get you into the textual user interfaces, and provide very nice, relatively easy to use windows, menus, editors, etc.
Then you'll want to look into code highlighting modules for python.
It's a fun process dealing with the limitations of textual interfaces, and you can learn a lot by going down this road. Good luck!
-Adam
I would recommend the excellent urwid toolkit (http://excess.org/article/2009/03/urwid-0984-released) - it's much easier to use than straight curses.
Well, what do you mean by a GUI? If you just want to create something that can be used on a console, look into the curses module in the Python standard library, which allows you to simulate a primitive GUI of sorts on a console.
Not quite a reference to a Python library, but The Craft of Text Editing by Craig A. Finseth might be of interest you.
A not very serious suggestions: a line editor can be implemented without curses.
These things are pretty primitive, of course, and not a lot of fun to work in. But they can be implemented with very little code, and would give you a chance to fool around with various schemes for maintaining the file state in memory pretty quickly.
And they would put you in touch with the programmers of the early seventies (when they had teletypes and the first glass teletypes, but after punched cards were a bit passe...).
Another option without curses is Python Slang
Newt is a library written on top of Slang.

What are good ways to make my Python code run first time? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm getting quite a few errors in my code. Consequently, I would like to be able to minimize them at the outset or see as many errors as possible before code execution. Is this possible and if so, how can I do this?
If you're having problems with syntax, you could try an editor with syntax highlighting. Until you get the feel for a language, simple errors won't just pop out at you.
The simplest form of debugging is just to insert some print statements. A more advanced (and extensible) way to do this would be to use the logging module from the std lib.
The interactive interpreter is a wonderful tool for working with python code, and IPython is a great improvement over the built-in REPL (Read Eval Print Loop).
If you actually want to step through your code, the python debugger is called pdb, which can be called from the command line, or embedded in your code.
If you're used to a fully integrated IDE, I would recommend using Eclipse with pydev, and PyCharm has a great commercial offering, with autocomplete, quick access to docs, and numerous shortcuts, among many other interesting features.
Here is some techniques to facilitate debugging in Python:
use interactive shell e.g., ipython. Python is a dynamic language you can explore your code as you type. The shell is running in the second window in my editor at all times.
copy-paste from the shell into docstrings a code that illustrates a dominant usage and corner cases of the function (class, module). doctest.testmod() placed in a if __name__=="__main__" section allows to test all docstrings in the module. doctest can be easily integrated with unittest.
use assert for stuff that can never happen.
print() can solve many debugging problems; logging module is suitable for long-living python processes.
write tests (not necessarily before your code), run them often (automatically or with one keystroke at most); nose provides extended test discovery and running features for unittest.
run pylint occasionally.
At this point there is a little use for a formal python debugger.
Winpdb is an external multi-platform GPL'ed GUI python debugger if you need one.
All the really cool stuff is easily demonstrated in the interactive interpreter. I think this might be the "gold standard" for good design:
Can you exercise your class interactively?
If you can do stuff interactively, then you can write unittests and doctests with confidence that it's testable, simple, reliable.
And, more important, you can explore it interactively. There's nothing better than the instant gratification that comes from typing code and seeing exactly what happens.
The compiled language habits (write a bunch of stuff, debug a bunch of stuff, test a bunch of stuff) can be left behind. Instead, you can write a little bit of stuff, explore it, write a formal test and then attach your little bit of stuff to your growing final project.
You still do overall design. But you don't squander time writing code that may or may not work. In Python you write code that works. If you're not sure, you play with it interactively until you're sure. Then you write code that works.
I am new to python, and have been trying several different debuggers. Here are the options I've come across so far:
Eclipse with Pydev - If you're already using eclipse, this is probably the way to go. The debugger works well, and is pretty featureful.
Komodo IDE - A light-weight python IDE. Basically a text editor + a debugger. It was really easy to figure out and be productive with immediately.
WinPDB - Trying this one next. Looks very featureful, and I get to use whichever editor I choose.
PDB - Haven't tried yet since I've read about how WinPDB is a better alternative.
Ipython with %run command - I've used IPython, but not as a debugger like this. I need to try this out. (Thanks for the tip, EOL)
Eric IDE - Also on the list to try.
Old-school print, assert statements - Simple, useful, but not a full solution.
Memory debugging - To debug memory problems, I've come across a few tools:
objgraph - Will generate png's of reference graphs. Neat. There's other functionality as well, like: import objgraph;print(objgraph.show_most_common_types(limit=10)) will print the top 10 most common types in memory.
gc module - Interact directly with the garbage collector.
heapy - Heap analyzer. For example: from guppy import hpy; hp = hpy(); print(hp.heap()) will print the most common types, their memory usage, etc.
This is a very incomplete list, but hopefully it's a good start.
Python provides a debugger which allows you to step through your code, inspect variables and manipulate them. Refer to http://pythonconquerstheuniverse.wordpress.com/category/python-debugger/ which can take you over the steps...
Also check the python standard library reference for pdb
http://www.python.org/doc/2.5.2/lib/module-pdb.html
Test early and test often.
This doesn't necessarily mean to jump into the test driven design pool head first (though that's not such a bad idea). It just means, test your objects and methods as soon as you have something that works. Don't wait until you have a huge pile of code before doing testing.
Invest some time in learning a testing framework. If it's trivial for you to type in a test case you'll more likely do it. If you don't have any sort of framework testing can be a pain so you'll avoid it. So, establish some good habits early on and you'll have fewer problems down the road.
More often than not, I just use a bunch of print statements.
page = grabpage(url)
print "Page content:", page
print "page type():", type(page)
It's sometimes useful to do something like:
debug = True
if debug: print "page content", page
..with this you can quickly disable all your debugging print statements by changing the debug variable to False.
While print-debugging will get you very far in most cases, sometimes it's difficult to debug things like loops, or a series of if/else/try/except/etc. For this a debugger that allows stepping though your code, and setting break-points is useful.
pdb is included with Python. Here is a good simple tutorial on it. You can even do things like changing variables during run-time (which the tutorial covers). A longer tutorial can be found here
There is a very nice GUI equivalent pdb - Winpdb
Basically you run winpdb myscript --arg 4 -b 4 then it loads the command in a terminal, shows you your code on the left, with the current, a list of local/global variables (and their values) and the current call-stack.
Then you can step though the code by clicking the Step (or F6). F5 runs the code. If you click next to the line numbers, it sets a break point, where the code will automatically step (when you press run).
I find it far easier to use, and it has lots of addition enhancements (like remote debugging, so you can run the backend portion (rpdb2) in the to-be-debugged application, and connect Winpdb to it (encrypted). It also has support for threading and other useful things not in PDB. You have access to a pdb-like console too.
I set up Python to automatically start the debugger when there's an uncaught exception, using this trick. That way, you can easily examine the state of the program without too much logging code. (Plus, to send me a Growl notification.)
Oh, and this way you can just create a break point in the code by adding
if answer == 42:
1/0
Using assert statement liberally.
Identifing errors before execution is the domain of static checking/analysis. I've had good luck using PyChecker for basic static checking of Python code.
The pycheesecake site has a very good summary of testing tools for Python.
The PyDev plugin for eclipse is my tool of choice. It recognizes simple syntax mistakes and indentation errors and underlines the error with a red line. It has a powerful debugger and even has a plugin called PyLint which warns you about dangerous code.
Edit:
It also has a user friendly stack trace on runtime errors, partial auto complete and some other nifty features.
Edit again:
I didn't notice that pydev was mentioned in the top post. I hope I brought something else to the table.
python -m pdb yourcode.py should do it.
Alternatively you can "import pdb" in your code and use pdb.set_trace() to set break points.
Refer the manual for more info: http://www.python.org/doc/2.5.2/lib/module-pdb.html
There is very nice GUI debugger for Python called Winpdb. Try it out.Built on wxWidgets library and multiplatform.
For a most integrated experience you can use full blown IDE like PyCharm:
http://blog.jetbrains.com/pycharm/files/2011/05/template-debug2.png
Eric4 IDE also has a great built-in debugger.
The IPython Python shell has a %pdb command that automatically invokes the debugger in case of problem. You can then inspect variables, step through the code, etc.
You can run your programs from IPython with the %run command.
More generally, as as been noted in some other answers, a good Python shell is your friend. And IPython is your best friend. :)

Categories