next up previous
Next: What is a Scripting Up: hello Previous: Running Python interactively


A first Python script: hello.py

Use your text editor to create a file called hello.py. Copy the following lines.

#! /usr/local/bin/python3

print('Hello, world!')

Be sure to get all of the punctuation right. The first line tells the shell that the file you have created is to be handed to the Python interpreter. This file is your script.

When you are satisfied the file is correct, save it. Then in your terminal window, set "execute" permission for the file with the command

chmod +x hello.py
(Be careful to put a space before and after the +x.) Then run your script with the command
hello.py
You should see the message, "Hello, world!" printed on your screen.

You can also run your Python script from the command line by typing

/usr/local/bin/python3 hello.py
or just python3 hello.py. If you want to do it this way, you don't need to put the #! /usr/local/bin/python3 line at the top of your file and you don't need to do the chmod.

When you are finished, exit the editor.



Subsections
next up previous
Next: What is a Scripting Up: hello Previous: Running Python interactively
Carleton DeTar 2017-08-10