Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Python as a CLI

00:00 In this lesson, I’m going to tell you a little bit more about how Python itself functions as a command line interface. Now, if you’re anything like me, you might be a little bit confused as to how Python can be both a command line interface and a programming language that lets you write command line interfaces. Well, don’t worry.

00:19 We’ll get that resolved through the course of this lesson.

00:23 Now I’m here in my Bash terminal and, as you can see, python is working just fine as a command line interface without any arguments at all. You just enter python into your terminal and you get the interactive interpreter where you can enter Python code and see it run.

00:39 So, that’s pretty darn cool. And of course, I’ve already been showing you many, many times already in this tutorial how you can use python with a file argument—so, you pass in a file to python and it will just run that file.

00:52 This is the "Python Main" function that I used in one of the very first sections, and it just prints out the number of arguments and the arguments in order.

01:01 But there are a lot of other things you can do with the Python command line interface and you can take a look at those by using the -h flag, or the --help long option.

01:12 As you can see, this as a whole heck of a lot of stuff that’s really interesting to dig into if you have the time. But what’s important to note is that there are a variety of options and arguments here, but that all of these options and arguments apply to the actual behavior of the Python compiler and interpreter because, remember, what python is actually doing when you run it is Python takes in some Python code and actually generates the corresponding bytecode, and then finally, machine code that your computer needs to actually do the tasks that your Python code says for it to do.

01:46 So, there are a bunch of options here that do all sorts of different things. You can have an option to avoid writing certain extra files that you might not always need on imports. You can get some debug output from the parser.

01:59 You can inspect stuff interactively—you can inspect your variables, and so on, interactively. You can do all sorts of different things: verbose output, version numbers.

02:09 But remember that what unifies all of these is that they are actually changing how python itself works, instead of changing how your code works.

02:17 These arguments are not going to be seen by your code. I have a slide that shows this relationship in a little bit more detail.

02:25 This slide shows you what the Python command line argument structure would look like for this arbitrary python command below. So python with the -B and -v options—so, verbose output and don’t write Python bytecode files on imports.

02:41 Running the file main.py with these two fake options—that don’t actually exist in my main.py file, but just for fun—two long options --verbose and --debug, and then two operands, or arguments, un and deux, which are—I believe—French for one and two.

02:58 So, these are, at the beginning, the actual Python options which affect how the Python command line interface runs. These work only on the system-level Python. And then the actual arguments in sys.argv are contained within and following this main.py.

03:16 So, you pass the Python file, and then those program options and program arguments are contained in the actual sys.argv that you could access in your code.

03:25 So, that’s how the arguments are divided up between the actual Python options and then the Python arguments that are passed into your Python code.

03:34 In the next section, I’ll get into how to roll your own command line interface entirely with Python from scratch.

Become a Member to join the conversation.