This tutorial is for Windows users who want to get their computer set up for developing with Python.
Python is a programming language that is easy to learn, yet powerful and flexible. Many professional software engineers use Python every day to do their job, for both large projects and small ones. Python is free, open source, and it runs on Windows, Mac, and Linux, as well as many other operating systems. It's a great language if you want to start learning how to write computer programs.
To effectively use Python on Windows, you need a few pieces of software:
Let's handle them one at a time.
Go to python.org and download the version of Python that you want. In these examples, I'm downloading Python 3.2.3.
Run the installer, and let it install to the default location: in this case, C:\Python32\
.
That was easy!
This is a bit more complicated. First, go to the Cygwin homepage, download setup.exe, and run it.
Cygwin is a program that can download and install other programs from the internet for you. You'll need to give it some information about your internet connection, although most of the time you can just accept the defaults and keep going.
Next, Cygwin will show you a long list of download sites. Each one is exactly the same: you can pick one at random. You do not need to pick the same download site shown in the screenshot. After you've picked one and clicked the "next" button, you'll see an upgrade warning, but that only applies to people upgrading from an older version of Cygwin. Since this is a new installation, we can ignore it.
Now, we get to select what software we want to install from the download site. We want to install three software packages: openssh
, git
, and curl
. For each one, use the search box to find the package, and then click on the word "Skip" so that it changes to a version number. Install the latest available version for each of these packages.
Next, Cygwin will tell you that you need to install certain other packages as dependencies. Basically, this means that if you want to use a certain package named A
, and A
relies on another package named B
in order to run correctly, then Cygwin will detect this and ask to install package B
as well. You can just hit "Next". At that point, Cygwin will start downloading and installing all the packages that you've requested, as well as all their dependencies. Depending on the speed of your internet connection, this may take a little while. When the middle of the three progress bars (the one marked "Total") is full, Cygwin is finished.
If you ask Cygwin to install an icon on your desktop, it will do so, creating a shortcut named "Cygwin Terminal". You'll use this icon to run your Python code, as well as to access the openssh
, git
, and curl
packages that you installed. Double-click the icon to run Cygwin Terminal, and you'll see a window like this pop up:
The exact text in your terminal window will be a little different, since it depends on the name of your computer, and the name of your user account. Now we can start using the terminal: if you don't know how, I suggest you read
this tutorial on how to use the command line.
First, let's use the which
command to find out where those packages are that we installed:
So it looks like the terminal can find ssh
and git
, but not python
. That's understandable, since we didn't use Cygwin to install Python. To tell Cygwin how to find Python, run the following command:
echo "PATH=\$PATH:/cygdrive/c/Python32" >> .bash_profile
Note that you're using the same directory that you installed Python into: if you're installing a different version of Python, then replace Python32
with the version of Python you have installed. You should only need to do this once: after you've run that command, the terminal will always be able to find Python again.
You're finally done installing and setting up Cygwin!
Lastly, you'll need a text editor. There are many good options, but I suggest Sublime Text: it's very powerful and useful, and it has an unlimited free trial. Please note that Microsoft Word is a word processor, not a text editor: you cannot use Microsoft Word for writing computer programs.
Installing Sublime Text is easy: just go to the Sublime Text homepage, download the installer, and install it. Sublime Text is not free software, but you can try it for as long as you like before you buy it. If you end up writing many programs using Sublime Text, I encourage you to buy a license.
Now that you're all set up, let's verify that everything is working properly! The first program that any programmer writes is called "Hello, World!", and all it does is write that text out to the terminal. So, let's write "Hello, World!" in Python! If your computer is set up properly, this should work properly:
$ python -c 'print("Hello, World!")'
Hello, World!
If that works, lets move on to writing a program into a file. Open up Sublime Text, and type in the following:
#!/usr/bin/env python
print("Hello, World!")
Save the file in your home directory (inside of C:\cygwin\home
) as hello.py
. Then open up the Terminal, cd to that directory, and run:
$ chmod a+x hello.py
$ ./hello.py
Hello, World!
And you should see the same Hello, World!
output that you saw before. Congratulations, you're ready to start learning Python!