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.
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.
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.
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.
This slide shows you what the Python command line argument structure would look like for this arbitrary
python command below. So
python with the
-v options—so, verbose output and don’t write Python bytecode files on imports.
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
--debug, and then two operands, or arguments,
deux, which are—I believe—French for one and two.
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
Become a Member to join the conversation.