Question 5.4: Running Python Programs from the Terminal Running programs f......

Running Python Programs from the Terminal

Running programs from within IDLE is fine, but sometimes you want to run a Python program from a Terminal window.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Use the python or python3 command in a Terminal, followed by the filename containing the program you want to run.

Discussion

To run a Python 2 program from the command line, use a command like this:

$ python myprogram.py

If you want to run the program using Python 3, then change the command python to python3. The Python program that you wish to run should be in a file with the extension.py.

You can run most Python programs as a normal user; however, there are some, especially those that use the GPIO port, that you need to run as super user. If this is the case for your program, prefix the command with sudo:

$ sudo python myprogram.py

See Also

Recipe 3.21 allows you to run a Python program as a timed event.

To automatically run a program at startup, see Recipe 3.20.

Related Answered Questions

Question: 5.24

Verified Answer:

Create a function that groups together lines of co...
Question: 5.23

Verified Answer:

Use the Python break statement to exit either a wh...
Question: 5.22

Verified Answer:

Use the Python while statement. The while statemen...
Question: 5.21

Verified Answer:

Use the Python for command and iterate over a rang...
Question: 5.20

Verified Answer:

Use one of the logical operators: and, or, and not...
Question: 5.19

Verified Answer:

Use one of the comparison operators: <, >, &...
Question: 5.17

Verified Answer:

Use the upper or lower function as appropriate. Fo...
Question: 5.18

Verified Answer:

Use the Python if command. The following example w...
Question: 5.15

Verified Answer:

Use the Python [:] notation. For example, to cut o...