installing and testing python

posted by tom / January 19, 2006 /

This should be pretty easy.

Windows
You have two options. The basic distribution of Python (2.4.2) is available here. However, there's also a free IDE (integrated development environment) available here. The latter option (ActivePython) is probably easier to work with, and doubtless has a lot of cool features. Let's go with that, why don't we.

Mac
OS X 10.2 and higher comes with Python pre-installed. You don't have to do anything — but I'd recommend that you go here, download the installer, and run it. For one thing, this will update your version of Python. For another, it will provide you with an IDE of your own (PythonIDE). More importantly, it'll provide a package manager, which will be important later.

Linux/Other
You guys probably know how to fend for yourselves. Source tarballs can be found here.

Testing It
Let's put together a Python program. I promised interesting applications, but it'd be a shame to skip past Hello World -- it's a rite of passage. Open up ActivePython or the PythonIDE and create a new file (File|New). Enter the following:

print "hello world"

It's hardly worth explaining, but I may as well: this single line calls the print function and passes it a string literal with the contents hello world. Don't worry about what a function is or what a string is or what a literal is (if you do know these things, feel free to feel smug about that fact). Just know that saying print "something" spits 'something' out to the screen, and that the quotation marks in the program aren't part of the output — in this case, they let Python know that it should treat the text hello world differently than it treats the text print.

Once you've got that entered (raise your hand if you're still working), save the file somewhere, then hit the "Run All" button in PythonIDE (Mac) or the "Check"/Little Running Guy button in ActivePython (Windows). ActivePython will pop up a "Run Script" box — just hit return.

The program should spit out the expected string in an output window (PythonIDE will create a new window; ActivePython sends output to the "Interactive Window" that should have opened when you started the program).

Astounding, isn't it? Well, don't worry, we'll be doing real programming soon. For now, just make sure that you understand how to create, load and save Python files — it's all done in exactly the same way as you'd manipulate documents in MS Word.

Also worth noting: these files are simply text files (usually with names ending in .py; that's just the convention). You can edit them in Notepad or TextEdit if you'd like, and run them from the command line by typing "python nameofprogram.py" (assuming you're in the same directory as the file, and the Python executable has been correctly placed in your path by the ActivePython or MacPython installer).

That's it for now. Next up, the meat of any programming language: variables, control structures and functions (not necessarily in that order). By the end of that lesson, you'll be ready to create some of the world's crappiest videogames. Let me know in comments if you had trouble with the installation or with Hello World.

Comments

Just so everyone (like people who install the unix distro who, I think, won't get an "icon" to "click" on their "desktop"), the name of the IDE that comes with the distribution is "idle" (as in Interactive DeveLopment Environment, and Eric Idle). "idle -e " opens a file for editing.

Posted by: ben wolfson on January 19, 2006 12:22 AM

When I hit run (in the windows version), a box pops up that says "Error: could not save file". I hit OK and then a Run Script box pops up. Hitting OK there closes that box, and nothing happens.

Posted by: Matt F on January 19, 2006 12:44 AM

whoops. In ActivePython, I think you have to save the file before running it (for some reason). Give that a shot. I'll update the post.

Posted by: tom on January 19, 2006 12:59 AM

Alright, works now. Thanks.

Posted by: Matt F on January 19, 2006 01:03 AM

Interesting choice of Python. Python is a really cool language, though I'm surprised you didn't choose PERL. PERL is, IMO, a lot easier to learn, because you don't really have to learn a lot of programming concepts to get something useful up and running. Of course, PERL has the tendency to be write-only (you should see the code that parses various venue's show listings that I wrote), so it could be hard to actually teach someone.


The most confusing thing about python is all the magic double-underscore stuff. I guess a beginner programmer probably doesn't need to worry about it, but I found it contradictory to Python's otherwise simple and straightforward syntax.


Apologies if I missed the blog entry that predicated this tutorial; it's not on the main page

Posted by: Here's a Hint on January 19, 2006 11:41 AM

Hey HaH, glad to have you along (the more programmers to identify my mistakes the better). The original post outlining my reasons for choosing Python is here -- I'll eventually link to it from this entry to provide easy navigation for googlers.

I agree that Python has some slight weirdness -- but it's certainly much easier to read than Perl. I'm still getting used to the latter (and I think I'll always hate having different comparison operators for strings and numeric variables). We probably won't get into too much language-specific weirdness; I'm going to keep things procedural, and focus on manipulating web forms (and the concepts necessary to do so).

Posted by: tom on January 19, 2006 11:54 AM

OK, the icon for ActivePython is supercute.

I find it disappointing that it sends the output of the program to an input line in an interpreter window, yet won't execute a dynamically generated command. Perhaps a safety/security decision? After I saw that the output of 'print "hello world"' was printed after the ">>>" prompt instead of looking like an output line, I tried 'print "print \"hello world\" "' and 'print "print \"hello world\" "' and those commands showed up correctly color-coded in the interpreter window as if they would execute but wouldn't run, even if I manually entered a return.

Posted by: Becks on January 20, 2006 02:18 PM

Becks, dynamically generated statements are executed with the "exec" statement, and dynamically generated expressions with the "eval()" builtin.

Eg (this is from an interactive IDLE session, and the statements aren't actually dynamically generated, but you'll get the idea):

>>> exec "print 'hi'"
hi
>>> a, b = range(2)
>>> eval('a + b')
1
>>> exec """\
def spam(*a):
return [e for (i,e) in enumerate(a) if i % 2]
"""
>>> eval('spam(1,2,3,"hello everyone", spam)')
[2, 'hello everyone']
>>>

I don't know what the ActivePython IDE looks like that you might think that just printing something might make the interpreter execute the printed statement.

eval() has two optional arguments, corresponding to global and local dictionaries (namespaces) providing the context in which to evaluate the code (you can execute help(eval) at the top level in an interactive environment for its behavior). The same is true of exec:

>>> exec "a = 3"
>>> a
3
>>> g = {}
>>> exec "a = 4" in g
>>> a, g['a']
(3, 4)
>>> L = {}
>>> exec "a = 7" in g, L
>>> a, g['a'], L['a']
(3, 4, 7)
>>>

Posted by: ben wolfson on January 20, 2006 09:42 PM

i finally got around to trying this, and i succeeded! i'm a programmer! finally.

Posted by: catherine on January 21, 2006 06:54 PM

Post A Comment

Name


Email Address


URL


Comments


Remember info?



Google Analytics