Python does not normally come pre-installed on Microsoft Windows. The standard installation location is c:\PythonXY where XY corresponds to the major and minor version of Python. For example, c:\Python27 is for Python 2.7 and c:\Python34 is for Python 3.4. Using Windows Explorer (or any file browsing utility you like), check whether one of these folders exists on your computer. If not, you will need to install Python yourself using the following steps:
- In your web browser, visit the Python download page ( https://www.python.org/downloads/).
- Near the top of the page, there are yellow buttons with labels like Download Python 3.4.2 and Download Python 2.7.8. Select the version you prefer to use for the course and click the button. This should either start downloading the file (named python-X.Y.Z.msi) to your browser download folder or bring up a dialog asking you where to store the file. This file is the installer and is an executable program. You may want to scan the downloaded file with an antivirus program just to be safe.
- To actually install Python, you will need to run the installer. You can either start it from the browser (e.g., in Firefox, you can double click on the downloaded file in the Downloads dialog), or from Windows Explorer or some other file browser (just navigate to the download folder and double click on the installer). Follow the instructions displayed by the installer. (I usually use all default settings.) If you choose to Install for everyone, you will be asked for administrator privileges during installation. Once the installer finishes, you can verify that c:\PythonXY has been created. (Python is reasonably safe to install in that the installer is not bloated with unrelated packages, and may be cleanly removed using the standard Windows "uninstall" procedure.)
There are two common ways to use Python on Windows. The command line interface is more similar to usage on Linux and Mac; the graphical interface provides some more functionality and includes an editor.
Command Line Interface
The Python installer creates a start menu group to simplify access to Python tools:
- click on the Start icon (first icon on the taskbar);
- click on All Programs (or just wait a bit);
- scroll down and click on Python 3.4 or Python 2.7;
- and click Python (command line) or Python (command line - 32 bit).
This should bring up a window displaying the Python version followed by the >>> prompt. You can type Python statements and see them executed in this window. Unfortunately, you cannot give an argument (like the name of a Python script) using this method.
To give Python command line arguments, you need to start a cmd window:
- click on the Start icon (first icon on the taskbar);
- enter cmd in the search field;
- and either click on the cmd.exe icon or just hit the Enter key.
This should bring up a window that shows a prompt that looks something like C:\Users\conrad>. This tells you where cmd, and therefore python, will look for files. If your Python script is in another location, you need to tell cmd. In the figure, I
(Note that Windows is case-insensitive and we can use python34 instead of Python34.) You can run Python multiple times in the same cmd window. Typically, you have both an editor and a cmd window open at the same time; you test a script by running it in the cmd window, modify it in the editor window, and repeat.
- started in folder c:\Users\conrad
- tried to look at the contents of a file named try.py
- told cmd to use a different drive with e:
- told cmd to use a different folder in the drive with cd \home\conrad\pc204
- tried to look at the contents of try.py again
- used c:\python34\python try.py to start a Python interpreter and execute try.py as a script
You can also run c:\python34\python with no arguments, which would bring up Python with an interactive >>> prompt. You can test some code by typing Python statements to the interpreter and examining the results. To exit from the interpreter, type either exit() or the two-key sequence of control-Z Enter. (In the latter case, you should see ^Z when you type control-Z.) This should bring you back to the cmd prompt.
You may want to simply type python rather than c:\python34\python each time. To do this, give the command set PATH=c:\python34;%PATH%. This makes cmd look for commands in c:\python34 in addition to the standard Windows folders (%PATH%). Unfortunately, the effect of the set command is limited to the cmd instance in which it was entered. If you want to make Python globally available in all cmdinstances, try reading How to set the path and environment variables in Windows.
Graphical Interface
Python has a built-in graphical interface called IDLE, Interactive DeveLopment Environment. There are better ones around, but IDLE comes as part of the standard Python distribution. To start IDLE,
(If you use AVG Antivirus software, it may claim that pythonw is infected. It is not. Just ask AVG for More options and then Allow execution.) To facilitate starting IDLE, you may want to create a shortcut, e.g., on your desktop. IDLE uses a fairly standard menu interface and is discussed (briefly) in lecture. One Day of IDLE Toying is a nice introductory tutorial.
- click on the Start icon (first icon on the taskbar);
- click on All Programs (or just wait a bit);
- scroll down and click on Python 3.4 or Python 2.7;
- and click IDLE (Python 3.4 GUI - 32 bit) or something similar.
For Python 2.7
To install Swampy, read the installation instructions. (The Windows section follows the Linux and Mac sections.) For Windows, Python and Tkinter are included in the installer. If either test procedure for Python or Tkinter in the Swampy installation instructions fails, please contact one of the PC204 instructors. I strongly recommend using the pip route for installing Swampy. A common pattern for using for Swampy modules in Python 2.7 is something like:
from swampy import TurtleWorld
For Python 3.4
As the Swampy instructions state, pip does not work for Python 3 yet. The simplest procedure I've found is:
- download and open the source zip file, swampy-2.1.5.python3.zip, in a file browser;
- open another file browser and navigate to c:\Python34\Lib\site-packages;
- drag swampy-2.1.5 from the zip file to site-packages;
- create a text file, swampy.pth, in site-packages with a single line of swampy-2.1.5.
There are a couple ramifications to the above installation procedure. First, the standard uninstall procedure may not remove c:\Python34 because Swampy is still present. If you do remove Python, it is safe to manually remove c:\Python34 afterwards. Second, the installation procedure causes Python to treat Swampy as separate top-level modules instead of a packages of modules. This means that the common usage in Python 3 is:
Note that there is no reference in the import to swampy at all.import TurtleWorld
Python searches a standard set of paths when importing modules. If you have a folder containing a set of modules, e.g., my_library, you can insert some code at the top of your script to tell Python to search additional folders. For example, if you want to import the my_module.py module from the my_library folder e:\home\conrad\my_library, then you can use the following:
The first two lines tell Python that there is an extra folder in which to search for modules. (Note that you need to double the backslashes because of the way Python treats the backslash escape character.) After that, the my_module module can be imported normally.
import sys sys.path.insert(0, "E:\\home\\conrad\\my_library") import my_module
Last updated: October 15, 2014