We will be running files containing Python code (Python "scripts") from the command prompt. You'll need to be able to navigate to those scripts using the command prompt so you can run them. In this section, we'll practice using these navigation commands.
The filesystem on your computer is like a tree made up of folders (also called "directories") and files. The filesystem has a root directory called C:\
, and most everything on your computer lives in subdirectories of this root directory (though you may also have separate drives, like D:\
and E:\
).
We often navigate the filesystem graphically by clicking on graphical folders. We can do the exact same navigation from the command line.
There are two commands that we'll be using at a command prompt to navigate the filesystem on your computer:
dir
cd
dir
lists the contents of a directory.
cd
moves you into a new directory (it stands for "change directory").
Let's practice using these commands.
On Windows Vista or Windows 7: click on the Start menu (the Windows logo in the lower left of the screen), type cmd
into the Search field directly above the Start menu button, and click on "cmd" in the search results above the Search field.
On Windows XP: click on the Start menu (the Windows logo in the lower left of the screen), click on Run...
, type cmd
into the text box, and hit Enter
.
Type each of these commands and hit Enter
:
dir
This lists all the files in your home directory.
cd C:\
This will change you into the C:\
directory.
dir
This lists the contents of the C:\
directory.
cd Users
This will change you into the Users subdirectory of the C:\
directory.
dir
You should see the names of all the files and directories in C:\Users
.
cd ..
..
means "parent directory", so this command moved you up to the parent directory. You were in C:\Users
, so now you are in C:\
, the root directory.
dir
This lists the contents of the root directory, confirming where you are.
You can use Tab to auto-complete directory and file names. So from inside the root directory, if you type cd Use
and hit Tab
, the command prompt will auto-complete the directory name, and you can then hit enter to change into the C:\Users
directory.
The command prompt maintains a command history. You can use the up arrow to cycle through old commands.
Note that the text that makes up the command prompt changes as you move around directories. The command prompt will always give the full directory path to your current directory.
Answer these questions. Experiment at the command line if you need to! If you aren't sure about an answer, ask a helper.
C:\Users
, and you want to get to C:\Users\jesstess\projects
, how would you do that?You've practiced using dir
and cd
to navigate your computer's filesystem from the command prompt.
Insert the CD or USB drive provided by an instructor. Using Windows Explorer (start it by pressing Windows key + "E"), drag the "workshop" folder onto the Desktop of your own computer to make your own copy of the class materials. Then you can remove the CD or USB drive and pass it along. If you can't use a CD or USB drive, download this file and have an instructor help you prepare it.
The Python programming language is "open-source" - no single person or company owns it, or can own it - it's for everyone to use. Some companies wrap it up together with useful related programs. We'll use one such packaging called Anaconda, published free of charge by Continuum Analytics, because that's the most reliable way to get everything we need installed.
Double-click the "workshop" folder you just copied to enter it, then enter the "windows" folder.
If your computer is 64-bit: Double-click "Anaconda-1.7.0-Windows-x86_64.exe" and accept all the defaults to install Python.
If your computer is 32-bit: Double-click "Anaconda-1.7.0-Windows-x86.exe" and accept all the defaults to install Python.
Ugh, you're already confusing me!: I know, that's not a beginner-level question! So try double-clicking "Anaconda-1.7.0-Windows-x86_64.exe". If that works, your computer is 64-bit, so go ahead and follow the 64-bit directions. If you get an error message saying something like "not supported by this processor", your computer is 32-bit, so follow those directions instead.
Next, back in the "Desktop\workshop\windows\install" folder in Windows Explorer, double-click the npp.6.4.5.Installer.exe
file. This will install Notepad++, a "text editor" good for writing programs with. (Your computer came with other "text editors" like Notepad and WordPad, but those editors will mess up your programs if you try to write with them.) Again, accept all the defaults that the installer suggests.
Like Python, this program is free and open-source.
We'll do some learning and practicing at a Python prompt (this is "interactive" because you are typing the code and hitting enter to run it yourself, instead of running it from a file). So let's practice starting and exiting Python:
On Windows Vista or Windows 7: click on the Start menu (the Windows logo in the lower left of the screen), type cmd
into the Search field directly above the Start menu button, and click on cmd
in the search results above the Search field.
On Windows XP: click on the Start menu (the Windows logo in the lower left of the screen), click on Run...
, type cmd
into the text box, and hit enter.
To start Python, type
python
at the command prompt and hit Enter
. You should see something like
Python 2.7.1 (r271:86832, ...) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>>
indicates that you are at a Python prompt. If it does, hooray! You can skip down to "Exiting Python".
Instead of the Python prompt, do you see a message like
'python' is not recognized as an internal or external command,
operable program or batch file.
Darn! The installation script from Goal #2 was supposed to add Python to your PATH, but that script doesn't work on every computer. (It's not your fault!) That means we have to add Python to the PATH by hand.
"What's the PATH"? It's a list of all the directories your computer should look in when you type a command like 'python'.
So here's what we do. Feel free to ask for help if you're having trouble.
Close your command prompt window
Get to System Properties
Open up "My Computer" by clicking on the Start menu or the Windows logo in the lower-left hand corner, and navigate to "My Computer" (for Windows XP) or "Computer" (For Vista and Windows 7).
Right-click on the empty space in the window, and choose Properties.
If you're using XP, a window labeled "System Properties" will pop up. Click the "Advanced" tab.
If you're not using XP, a window labeled "View basic information about your computer" will appear.
Edit the Path
Within System Properties, make sure you are in the tab labeled "Advanced".
Click the button labeled "Environment Variables".
In this window, the screen is split between "User variables" and "System variables". Within "System variables", scroll down and find the one labeled Path. Click the "Edit..." button.
In the "Variable value" box, scroll to the end. Add the following text, and hit OK. Make sure to include the semicolon at the start!
;c:\python27;c:\python27\scripts
Hit "OK" to close out the system properties window.
To test your new PATH, open up a new command prompt; it needs to be a new command prompt because the changes you just made didn't take affect in prompts that were already open. Now go back to the beginning of Goal #3 and start again. This time you should be able to skip this middle section!
Exit the Python prompt by typing
exit()
and hitting enter. Now you're back at the Windows command prompt (C:).
What is the difference between a command prompt and a Python prompt? If you're not sure, ask a helper.
Practice these steps until you feel comfortable navigating to a command prompt, starting Python, and exiting Python.
Next you need to get Notepad++, the text editor that you just installed, ready to work with Python. (Notepad++ is different from the Notepad that came with Windows - plain old Notepad will make a mess of your programs if you try to use it.)
First, start Notepad++:
Now, follow these steps to tell the editor to use spaces instead of tab marks when you indent:
That's it! Now, you can hit tab to indent your code, and that indentation will actually be made of spaces. This change will help you use spaces consistently, so that Python doesn't get confused about whitespace.
If you'd rather use a different editor for text, check with an instructor before moving on to make sure it will work for the weekend.
Interactive Python programming at a Python prompt is great for short pieces of code and for testing ideas. For longer code, it can be easier to save the code in a file, and execute the contents of that file (aka a Python script). In this section, we'll practice running Python scripts.
Add the following line to your new text file:
print "Hello World!"
Save the script as hello.py in your Desktop directory. The .py
extension indicates that this file contains Python code.
Start a new command prompt. See the terminal navigation on Windows instructions for the steps to do this. Recall that a terminal prompt will look like C:\
and a Python prompt will look like >>>
. Make sure you are at a terminal prompt and not a Python prompt; if you are at a Python prompt, you can type exit()
on a line by itself and then hit Enter
to exit Python and return to a terminal prompt.
Navigate to your Desktop directory from a command prompt, using the dir
and cd
commands. See the terminal navigation on Windows instructions for a refresher on using these commands. Don't hesitate to get help from a staff member on this step if you need it -- it's a new way of navigating your computer, so it may be unintuitive at first!
Once you are in your Desktop directory, you'll see hello.py in the output of dir.
Type
python hello.py
and hit Enter
. Doing this will cause Python to execute the contents of that script -- it should print "Hello World!" to the screen. What you've done here is run the Python application with an argument -- the name of a file, in this case "hello.py". Python knows that when you give it a file name as an argument, it should execute the contents of the provided file. You get the same result as if you typed
print "Hello World!"
at a Python prompt and hit Enter
.
You created and ran your first Python script!
You'll spend most of this workshop using a Python tool called the IPython Notebook. This lets you run IPython cells, which are like the interactive Python prompt you tried out in Goal #3, right within your browser.
Open a command window (see Goal #1) and use cd
to navigate into the workshop
directory:
cd Desktop
cd workshop
Type
ipython notebook
and hit Enter
.
Your browser should come up with a page called "IP[y]: Notebook". That's your IPython Dashboard; it has a list of notebooks. Click "notebook_practice" and follow the instructions on the notebook that it brings up.
Go back to the IPython Dashboard page in your browser, and click the "day1.1" notebook. Follow along through that tutorial. Remember to ask lots of questions, to the instructors and to the other students - we're all here to help each other!
After "day1.1", you'll go through "day1.2" and then part of "exercises". We'll leave the rest for tomorrow!