Table of Contents
What Is the Standard Command to Execute a Python File like prog.py?
Learn the standard and most common command to run a Python script from your terminal. This guide explains how to use python prog.py to invoke the interpreter and execute your code, a fundamental skill for any Python developer.
Question
Which command is the most common way to run a script named prog.py from a terminal?
A. python prog.py
B. run prog.py
C. exec prog.py
D. compile prog.py
Answer
A. python prog.py
Explanation
Invokes the Python interpreter with the script.
The command python prog.py is the standard and most universally recognized way to execute a Python script from a command-line interface.
This command instructs the operating system’s shell to find the python interpreter executable and then pass the file prog.py to it as an argument. The interpreter takes over, reads the contents of prog.py, compiles the source code into an intermediate form called bytecode, and then executes that bytecode. This process runs your script and produces its output directly in the terminal. You must be in the same directory as the script file or provide the full path to it.
The other options are incorrect for the following reasons:
B. run prog.py: The command run is not a standard utility in most command-line shells (like Bash on Linux/macOS or Command Prompt on Windows). While some specific IDEs or environments might use a run command, it is not the general way to execute a script from a terminal.
C. exec prog.py: exec is a shell command that replaces the current shell process with the command that follows it. While it can be used to run programs, it’s not the standard or intended way to simply execute a Python script and has different process management implications. It is not typically used for this purpose.
D. compile prog.py: This is not a valid command. While Python code is compiled, this happens implicitly when you run it. There is a way to manually compile a file to bytecode (e.g., python -m py_compile prog.py), but this action creates a .pyc file and does not execute the script.
Python Basics: Learn, Apply & Build Programs certification exam assessment practice question and answer (Q&A) dump including multiple choice questions (MCQ) and objective type questions, with detail explanation and reference available free, helpful to pass the Python Basics: Learn, Apply & Build Programs exam and earn Python Basics: Learn, Apply & Build Programs certificate.