Python Setup

Python is free and open source, available for all operating systems from python.org. If not already installed, this article will help you with your python setup.

Python is already installed on many operating systems.

For this article, I have used some examples from Google’s Educational Materials found here as one of the sources to help you with python setup.

Is Python installed on my computer?

Most operating systems other than Windows already have Python installed by default

To check that Python is installed, open a command line (typically by running the “Terminal” program) and type “python -V”

If you see some version information, then you have Python installed already.

However, if you get “bash: python: command not found”, there is possibility that Python is not installed on your computer.

Please take a look at this excellent post at diveintopython.net where they describe how to install Python on various operating systems.

Python Interpreter

When you start the Python in interactive mode, it prompts for the next command, usually three greater-than signs (“>>> “).

The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt, eg:

To run the Python interpreter interactively, just type “python” in the terminal:

??$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

>>> 1 + 1
2
>>> you can type expressions here .. use ctrl-d to exit

How can I run my Python programs?

The most simplest way to run python program is by typing “python helloworld.py”

If the “execute bit” is set on a .py (python extension) file, it can be run by name without having to type “python” first.

Set the execute bit with the “chmod” command like this:
$ chmod +x hello.py.

Now you can your program as ./hello.py

How do I install python Windows?

Doing a basic Python install on Windows is easy, just go to the python.org download page, select a version such as 2.7.

Run the Python installer, taking all the defaults.

This will install Python in the root directory and set up some file associations.

With Python installed, open a command prompt (Accessories > Command Prompt, or type ‘cmd’ into the run dialog)

You should be able to run the helloworld.py python program by typing “python” followed by “helloworld.py”

To run the Python interpreter interactively, select the Run command from the Start menu, and type “python”,this will launch Python interactively in its own window.

On Windows,use Ctrl-Z to exit (on all other operating systems it’s Ctrl-D to exit)

Text Editors

Before we write Python programs in source files, we need an editor to write the source files.

For programming in Python, there are tons of editors to use.

Pick one that works for your platform.

Which editor you choose depends on how much experience you have with computers, what you need it to do, and on which platform you need to do it.

At very least, you want a text editor with a little understanding of code and indentation (Notepad ++, Smultron, TextEdit, Gedit, Kate, Vi, etc..)

How do I write Python programs

A Python program is just a text file that you edit directly.

In the command line (or terminal) you can run any python program you want, just as we did above with “python helloworld.py”

At the command line prompt, just hit the up-arrow key to recall previously typed commands, so it’s easy to run previous commands without retyping them.

To try out your editor, edit the the helloworld.py program.

Change the word “Hello” in the code to the word “Howdy”.

Save your edits and run the program to see its new output.

Try adding a “print ‘yay!’” just below the existing print and with the same indentation.

Try running the program, to see that your edits work correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *