Launch Terminal.app
(Mac) or whatever the bash
terminal or commandline prompt program is (UNIX). From the prompt, type python
followed by return/newline:
$ python
Python 2.7.11 (default, Jun 7 2016, 10:09:37)
...
If the version is 2.7.10, then you are probably using the older preinstalled version of Python. That's okay for now, but keep that in mind that you will probably want to do the brew install python
installation from setup at some point.
Now, from the Python prompt >>>
(we are no longer in bash
), type 500+1
followed by newline. You should see something like this:
>>> 500+1
501
>>>
Python has evaluated the expression and printed the result back to the screen. It is as if we had done this, which gives us the same result:
>>> print 500+1
501
>>>
The Python interactive shell prints all expression values immediately.
Go to a suitable directory on your disk, or create one, such as /Users/YOURID/msan501/inclass
. Now create a text file called hello.py
that contains exactly one line:
500+1
This is exactly what you typed in first in the interactive Python shell. Save the file in the inclass
directory.
Here are solutions to the most common errors:
- Do not put
.txt
at the end of the file name - Do NOT use M$ Word or any other word processor; You think it's text but it's not. There are lots of text editors out there including Mac's
TextEdit.app
. Just make sure save as plain text not "rich text". There are also plenty of text editor such as Sublime and TextMate. (If you are really hard-core, you will learnvi
oremacs
, which you will see me use in class.) - When all else fails, download hello.py from github.
Once you get the file written to the disk, you should be able to jump to that directory using the commandline shell. Use ls
to get a directory listing:
$ cd /Users/YOURID/msan501/inclass
$ ls
hello.py
Now, we're going to run that program/script:
$ python hello.py
$
We do not get any output. This is a critical difference. The interactive Python shell immediately prints expression values because it is interactive. When you run a file from the commandline, it assumes you wanted to execute like a script in batch mode. That is why we do not get any output.
Now edit that file and change it to
print 500+1
Save the file and rerun it. Now you should see:
$ python hello.py
501
$
Now, we're going to do the exact same thing except using the development environment PyCharm.
- Launch PyCharm and then under
File
menu, tell it to open a directory withOpen...
menu item. - Navigate to your
/Users/YOURID/msan501/inclass
directory and click okay. You should see yourhello.py
in theProject
pane of the development environment. - Double-click on it to open it in the editor pane.
- Right click in the editor window and select
Run hello
item. Another pane will open on the bottom of the IDE that looks something like:
/usr/local/bin/python /tmp/inclass/hello.py
501
Process finished with exit code 0
Finally, let's create a Python file from within the development environment.
- Choose
File:New...
and select itemPython File
in the submenu. It will pop up a dialog box. - Enter filename
hi.py
, which will then bring up an empty editor window. - Type
print "hi"
followed by newline in the editor window. - Right-click and select
Run hi
and, once again, you should see output in the bottom console.
You should be able to test out small programs or Python snippets very very quickly. Rehearse these procedures until they are second nature.