Get startedGet started for free

Python on the command line

1. Python on the command line

Although data processing is more productive on the command line, complex data work like predictive modeling is still easier in Python. In this chapter, we will learn how to run Python on the command line, so that we can combine the best of both worlds.

2. Python basics

Python comes pre-installed with MacOS and Linux, but needs to be installed for Windows. The most common way to interact with Python is via GUI interfaces like Jupyter Notebook, but the more efficient way is to access Python directly on the command line. Let's explore how to do this.

3. Using Python documentation

man python calls up the documentation and option flags. Since Python 2 and Python 3 have different syntax, it's helpful to check on the Python version by using dash-capital-V or dash-dash-version. Here, we have Python 3.5.2 installed.

4. Using Python documentation

Likewise, it is possible to have multiple copies of the same version of Python co-existing on your machine. To see which Python version you're using, type which python. Here, we're using the native Python in user bin.

5. The Python interactive session

One way to interact with Python on the command line is with an interactive Python session by typing python in the terminal and pressing Enter. You will know this is a success if you see a printout with the Python version, followed by the cursor blinking after 3 re-direct signs.

6. The Python interactive session

The 3 re-direct signs serve as a reminder that you are inside the interactive session. As long as you are inside the session, you can only write Python syntax. For example, we can type print open parenthesis quotation marks hello world end quotation marks end parenthesis, which is Python syntax. The terminal will echo back hello world. To exit the session, type exit open close parentheses and press Enter. Notice that in the next line, the three re-directs are replaced with a dollar sign, which indicates we're back in normal command line.

7. Python interactive session alternative

The interactive session is easy to use, but it isn't helpful for code reproducibility. To execute the same Python command multiple times and on-demand, we need a second way to use Python on the command line. This is done by saving code in a Python script and calling this script when we are ready to execute. Let's dig further.

8. Python script execution on the command line

We can create the Python file using any text editor like Sublime, nano, Vim, or Emacs. Make sure that the file ends in a dot-py extension.

9. Python script execution on the command line

If our script is very short, we can simply echo this command and re-direct it into the Python file. This one-liner does two things: It instantiates a new file called hello underscore world dot py and populates the file with the print command. The benefit of this method is that we never leave the command line causing less disruption to our workflow. Let's sanity check and print the file content.

10. Python script execution on the command line

With the Python script created, we still need to call it to execute. First, we check that we are in the same file directory as the Python script we created. Then, we tell our terminal to execute this file using Python by calling `python` followed by the Python file hello underscore world dot py. Now, our terminal echoes hello world back to us.

11. Let's practice!

Congratulations! We have begun our journey on integrating Python into our command line data workflow!