How to Invoke and Use the Python Interpreter
Photo By Brecht Corbeel on Unsplash
The Python interpreter is the core program that executes Python code. Typically, this interpreter is installed in the operating system’s default directory.
On Unix or Linux systems, the default location is /usr/local/bin/python3.13 (The Python Version You Have). You can check its availability by opening the terminal and typing the following command :”
python3.13
If the command runs successfully, you will enter Python’s interactive mode, indicated by the triple greater-than sign (>>>). However, keep in mind that the directory where the interpreter is located may vary depending on the installation configuration.
For example, some systems use an alternative directory such as /usr/local/python. If you’re unsure, it’s best to consult your system administrator or Python instructor.
On Windows operating systems, if you installed Python via the Microsoft Store, the python3.13 (The Python Version You Have) or python command is usually available directly in the Command Prompt. Additionally, if you’re using the Python Launcher for Windows (py.exe), you can simply run the command :
py
To call the interpreter. This method is very useful as it simplifies the management of multiple Python versions on a single system.
To exit Python’s interactive mode, you can type the end-of-file character: Ctrl-D on Unix/Linux and Ctrl-Z on Windows, then press Enter. If that doesn’t work, you can also exit using the command :
quit()
Or :
exit()
Both commands function the same way and will end the Python interpreter session with an exit status of zero, which means the session ended normally without any errors.
The Python language interpreter supports interactive line editing features such as :
- Navigation using arrow keys
- Substitution from previous command history
- Auto-completion
These features are typically available on systems that support the GNU Readline library. To check whether your system supports this feature, simply press Ctrl-P at the Python prompt.
If you hear a beep or see the previous command appear, it means the feature is active. However, if nothing happens or you see the symbol ^P, the feature is unavailable, and you’ll have to rely on the Backspace key to edit the current line.
The Python interpreter can be run not only in interactive mode but also to execute scripts or single-line commands. For example, you can run a Python script from a file by typing :
python3.13 script.py
If you want to execute a Python command directly from the terminal, you can use the -c option followed by the command :
python3.13 -c "print('Hello, World!')"
Since Python commands may contain special characters or spaces, it is highly recommended to enclose the command in quotation marks.
In addition, Python provides a way to run a module as if you were typing its name on the command line. For example :
python3.13 -m http.server
The command above will start a simple HTTP server using Python’s built-in module. You can also add the -i option to enter interactive mode after the script finishes executing :
python3.13 -i script.py
This approach is very useful for debugging or further exploration after the script has been executed.
Passing Arguments to the Interpreter
When you run a Python script with additional arguments, the interpreter automatically converts all those arguments into a list of strings and stores them in the sys.argv variable from the sys module. You can access it by using :
import sys
print(sys.argv)
The first element in sys.argv is the name of the script file itself. If you run the interpreter without a script, the value of sys.argv[0] will be an empty string. If you use -c, the value will be ‘-c’, and if you use -m, the value will be the name of the module being executed.
Additional options written after -c or -m will not be processed by the interpreter, but will be passed directly to the script or module.
Interactive Mode
When the interpreter reads input from a tty (terminal), it enters interactive mode. In this mode, you will see the primary prompt >>> each time the interpreter is ready to accept a new command.
If you type a multi-line construct such as an if block, you will see the secondary prompt …, for example :
>>> the_world_is_flat = True
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
...
Be careful not to fall off!
Interactive mode is very useful for code exploration, function testing, or learning Python in a hands-on way.
The Python interpreter doesn’t just execute code it also creates an execution environment. One important aspect of this environment is the source file encoding.
By default, Python assumes all source files use UTF-8 encoding. This means you can use characters from various languages around the world in string literals, comments, and even variable names as long as your text editor supports UTF-8 and uses an appropriate font.
To use a different encoding, you must declare it on the first line of your Python file like this :
# -*- coding: cp1252 -*-
If your file uses a shebang line at the top (e.g., #!/usr/bin/env python3), then the encoding declaration must be written on the second line.
Specifying the correct encoding is important to avoid character reading errors, especially when working with files that contain non-ASCII text.
That concludes the article Using the Python Interpreter in the Python Programming Language that Mangcoding would like to share. We hope this article is helpful and provides you with new insights. If you have any constructive feedback or suggestions, feel free to leave a comment or contact us via email or Mangcoding’s social media channels
Source : Menggunakan Interpreter Python