Getting Started with Python on Ubuntu - Running from the Command Line

Overview

Up to this point, you have run your Python scripts from inside Geany. Doing so is fine when a program is under development. Once development has been completed, there are many other options available to you, depending on your needs.

In this lab, you will explore several different approaches, including the Python command, directly using the shebang, absolute paths, and relative paths. You will also redirect standard input (stdin) and standard output (stdout) to and from your program and disk files, which really helps harness the power of CLI programs. As before, the focus will be on debugging and problem solving.

In Task 1, you will explore running your script from the command line using Python. You’ll also work with stdin, stdout, and the time module.

In Task 2, you’ll examine other ways of running your scripts

outcomes

In this lab, you will learn to:

  • Run a Python script from the command line using the Python command.
  • Introduce modules, beginning with time.
  • Use a shebang to run a Python script by itself from the current working directory.
  • Explore running a Python script using an absolute and relative paths.
  • Configure the $PATH variable to run your program without specifying a path.
  • Explore standard input and standard output and redirection.

Key terms and descriptions

module
A module allows you to organize your Python code into libraries that you can reuse in multiple scripts.
$PATH
The PATH variable is a special variable in Linux that tells Linux where key programs and scripts are located to allow you to use them anywhere on a system so you do not have to specify the path
sheband
A shebang is a special line put in scripts to tell Linux what interpreter to use to run a script.
stdin and stdout
stdin and stdout are the standard input and output that allows a script to receive input and display output.