How do I open the command line interpreter?
At this point, you should have a working Python 3 interpreter at hand. If you need help getting Python set up correctly, please refer to the previous section in this tutorial series. Show
Here’s what you’ll learn in this tutorial: Now that you have a working Python setup, you’ll see how to actually execute Python code and run Python programs. By the end of this article, you’ll know how to:
It’s time to write some Python code! Hello, World!There is a long-standing custom in the field of computer programming
that the first code written in a newly installed language is a short program that simply displays the string The simplest Python 3 code to display You will explore several different ways to execute this code below. Using the Python Interpreter InteractivelyThe most straightforward way to start talking to Python is in an interactive Read-Eval-Print Loop (REPL) environment. That simply means starting up the interpreter and typing commands to it directly. The interpreter:
The session continues in this manner until you instruct the interpreter to terminate. Most of the example code in this tutorial series is presented as REPL interaction. Starting the InterpreterIn a GUI desktop environment, it is likely that the installation process placed an icon on the desktop or an item in the desktop menu system that starts Python. For example, in Windows, there will likely be a program group in the Start menu labeled Python 3.x, and under it a menu item labeled Python 3.x (32-bit), or something similar depending on the particular installation you chose. Clicking on that item will start the Python interpreter: The Python interpreter (REPL) running inside a terminal window.Alternatively, you can open a terminal window and run the interpreter from the command line. How you go about opening a terminal window varies depending on which operating system you’re using:
Using your operating system’s search function to search for “command” in Windows or “terminal” in macOS or Linux should find it. Once a terminal window is open, if paths have been set up properly by the Python
install process, you should be able to just type This example is from the Windows Command Prompt window:
If you are not seeing the Executing Python CodeIf you are seeing the prompt, you’re off and running! The next step is to execute the statement that displays
The interpreter’s response should appear on the next line. You can tell it is console output because the >>>
If your session looks like the above, then you have executed your first Python code! Take a moment to celebrate. Congratulations!Did something go wrong? Perhaps you made one of these mistakes:
(You will see in an upcoming section why this matters.) If you got some sort of error message, go back and verify that you typed the command exactly as shown above. Exiting the InterpreterWhen you are finished interacting with the interpreter, you can exit a REPL session in several ways:
Running a Python Script from the Command LineEntering commands to the Python interpreter interactively is great for quick testing and exploring features or functionality. Eventually though, as you create more complex applications, you will develop longer bodies of code that you will want to edit and run repeatedly. You clearly don’t want to re-type the code into the interpreter every time! This is where you will want to create a script file. A Python script is a reusable set of code. It is essentially a Python program—a sequence of Python instructions—contained in a file. You can run the program by specifying the name of the script file to the interpreter. Python scripts are just plain text, so you can edit them with any text editor. If you have a favorite programmer’s editor that operates on text files, it should be fine to use. If you don’t, the following are typically installed natively with their respective operating systems:
Using whatever editor you’ve chosen, create a script file called Now save the file, keeping track of the directory or folder you chose to save into. Start a command prompt or terminal window. If the current working directory is the same as the location in which you saved the file, you can simply specify the filename as a
command-line argument to the Python interpreter: For example, in Windows it would look like this:
If the script is not in the current working directory, you can still run it. You’ll just have to specify the path name to it:
In Linux or macOS, your session may look more like this:
A script file is not required to have a
But giving Python files a Interacting with Python through an IDEAn Integrated Development Environment (IDE) is an application that more or less combines all the functionality you have seen so far. IDEs usually provide REPL capability as well as an editor with which you can create and modify code to then submit to the interpreter for execution. You may also find cool features such as:
IDLEMost Python installations contain a rudimentary IDE called IDLE. The name ostensibly stands for Integrated Development and Learning Environment, but one member of the Monty Python troupe is named Eric Idle, which hardly seems like a coincidence. The procedure for running IDLE varies from one operating system to another. Starting IDLE in WindowsGo to the Start menu and select All Programs or All Apps. There should be a program icon labeled IDLE (Python 3.x 32-bit) or something similar. This will vary slightly between Win 7, 8, and 10. The IDLE icon may be in a program group folder named Python 3.x. You can also find the IDLE
program icon by using the Windows search facility from the start menu and typing in Click on the icon to start IDLE. Starting IDLE in macOSOpen Spotlight Search. Typing Cmd+Space is one of several ways to do this. In the search box, type In the terminal window, type Starting IDLE in LinuxIDLE is available with the Python 3 distribution but may not have been installed by default. To find out whether it is, open a terminal window. This varies depending on
the Linux distribution, but you should be able to find it by using the desktop search function and searching for If you get an error saying The method for installing apps also varies from one Linux distribution to the next. For example, with Ubuntu Linux, the command to install IDLE is
Follow whatever procedure is appropriate for your distribution to install IDLE. Then, type Whew! Using IDLEOnce IDLE is installed and you have started it successfully, you should see a window titled Python 3.x.x Shell, where 3.x.x corresponds to your version of Python: The The interpreter behaves more or less the same as when you ran it directly from the console. The IDLE interface adds the perk of displaying different syntactic elements in distinct colors to make things more readable. It also provides context-sensitive help. For example, if you type One other feature IDLE provides is statement recall:
You can also create script files and run them in IDLE. From the Shell window menu, select File → New File. That should open an additional editing window. Type in the code to be executed: From the menu in that window, select File → Save or File → Save As… and save the file to disk. Then select Run → Run Module. The output should appear back in the interpreter Shell window: OK, that’s probably enough Once both windows are open, you can switch back and forth, editing the code in one window, running it and displaying its output in the other. In that way, IDLE provides a rudimentary Python development platform. Although it is somewhat basic, it supports quite a bit of additional functionality, including code completion, code formatting, and a debugger. See the IDLE documentation for more details. ThonnyThonny is free Python IDE developed and maintained by the Institute of Computer Science at the University of Tartu, Estonia. It is targeted at Python beginners specifically, so the interface is simple and uncluttered as well as easy to understand and get comfortable with quickly. Like IDLE, Thonny supports REPL interaction as well as script file editing and execution: Thonny performs syntax highlighting and code completion in addition to providing a step-by-step debugger. One feature that is particularly helpful to those learning Python is that the debugger displays values in expressions as they are evaluated while you are stepping through the code: Thonny is especially easy to get started with because it comes with Python 3.6 built in. So you only need to perform one install, and you’re ready to go! Versions are available for Windows, macOS, and Linux. The Thonny website has download and installation instructions. IDLE and Thonny are certainly not the only games going. There are many other IDEs available for Python code editing and development. See our Python IDEs and Code Editors Guide for additional suggestions. Online Python REPL SitesAs you saw in the previous section, there are websites available that can provide you with interactive access to a Python interpreter online without you having to install anything locally. This approach may be unsatisfactory for some of the more complicated or lengthy examples in this tutorial. But for simple REPL sessions, it should work well. The Python Software Foundation provides an Interactive Shell on their website. On the main page, click on the button that looks like one of these: Or go directly to https://www.python.org/shell. You should get a page with a window that looks something like this: The familiar Here are a few other sites that provide Python REPL:
ConclusionLarger applications are typically contained in script files that are passed to the Python interpreter for execution. But one of the advantages of an interpreted language is that you can run the interpreter and execute commands interactively. Python is easy to use in this manner, and it is a great way to get your feet wet learning how the language works. The examples throughout this tutorial have been produced by direct interaction with the Python interpreter, but if you choose to use IDLE or some other available IDE, the examples should still work just fine. Continue to the next section, where you will start to explore the elements of the Python language itself. Which command is a commandCommand line interpreter (-CL or -CLI)
Use the Command Line Interpreter adapter command ( -CL or -CLI ) to specify the command line interpreter used to execute the batch file or shell script.
What is the command interpreter in Windows?The Windows command shell is actually an application built into the Windows operating system. CMD.exe is the command interpreter that accepts your commands and executes them in the way you want.
What is commandThe shell is the Linux command line interpreter. It provides an interface between the user and the kernel and executes programs called commands. For example, if a user enters ls then the shell executes the ls command.
|