how to run python scripts Flashcards
What is a script in computing?
A script is a text file containing a logical sequence of orders expressed in a scripting language, used to accomplish a specific task.
What is a scripting language?
A programming language that allows you to manipulate, customise, and automate tasks, typically interpreted at runtime.
Why is Python often referred to as an interpreted language?
Python programs are executed by an interpreter at runtime rather than being compiled beforehand.
Why are Python programs often called scripts?
Because they are interpreted and can consist of sequential code, although Python programs can also be complex applications.
What is the key difference between a script and a module in Python?
- Script: Contains executable code, typically run directly.
- Module: Contains importable code designed to be used in other Python files.
What is importable code?
Code that defines something, such as functions or classes, without performing specific actions.
What is executable code?
Code that performs specific actions, like function calls, loops, and conditionals.
What is an entry-point script?
A top-level Python program containing executable code.
What file extension do Python scripts and modules typically use?
.py
.pyw extension used on Windows for Python applications that should use the pythonw.exe launcher.
How do you create a Python script?
Use a Python-friendly code editor or IDE to write a program and save it with a .py extension.
How do you run a Python script using the python
command on Windows PowerShell?
- Open PowerShell.
- Type
python .\filename.py
orpy .\filename.py
- Press Enter.
How do you run a Python script using the python
command on Linux or macOS?
- Open the shell.
- Type
python ./filename.py
orpython3 ./filename.py
(if python3 is required). - Press Enter.
What should you do if the Python script doesn’t run from the command line?
- Check if Python is in your system’s PATH.
- Verify the file location of the script.
- Use python3 if required on some Linux or macOS systems.
What does the py
command do on Windows?
Triggers the py.exe
launcher for running Python console applications.
Provide an example of a basic Python script and its execution.
Script:
print("Hello, World!")
Filename: hello.py
Execution:
Windows PowerShell: python .\hello.py
Linux/macOS shell: python ./hello.py
What is redirection in the context of terminal or shell applications?
Redirection is a feature that allows you to send the output of a command to a file instead of displaying it on the screen.
How do you redirect the output of a Python script to a file?
Use the > symbol followed by the filename:$ python hello.py > output.txt
What happens to the output file when using > for redirection?
If it does not exist, the shell automatically creates the file. If it already exists, the shell overwrites the file’s old content with the new output.
How do you append the output of a Python script to the end of an existing file?
Use»_space; instead of >:$ python hello.py >> output.txt
What is the difference between > and»_space; in shell redirection?
> : Overwrites the file’s content.
>: Appends the output to the file.
How would the file output.txt
look after two consecutive executions of python hello.py >> output.txt
?
It would contain the phrase “Hello, World!” twice, each on a new line.
How can you run a Python script directly on Windows using its filename?
Enter the filename at the command prompt:PS> .\hello.py
Why does a new terminal window briefly appear when running Python scripts directly on Windows?
Windows associates .py files with python.exe, which opens a new terminal window for execution. The new terminal window closes automatically after execution, making it difficult to view the program’s output.
What happens when you try to run a Python script directly on Linux or macOS without preparation?
You get a “Permission denied” error due to Unix systems prioritising security.
How do you make a Python script executable on Linux or macOS?
Use the chmod
command:$ chmod +x hello.py
Why might you encounter a “syntax error near unexpected token” error after making a script executable?
The operating system doesn’t know which interpreter to use and tries to execute the script with the shell instead.
How can you specify which interpreter should run your Python script on Unix systems?
Add a shebang line at the start of your script, e.g.:#!/usr/bin/env python3
What is a shebang in a script file?
A special comment at the beginning of a file that specifies which program should execute the script.
Provide two ways to specify the interpreter in a shebang.
- Absolute path to the interpreter:
#!/usr/bin/python3
- Using the env command for portability:
#!/usr/bin/env python3
Why is using #!/usr/bin/env python3
considered more portable than #!/usr/bin/python3
?
The env
command locates the Python interpreter dynamically, ensuring compatibility across Unix systems where the Python path may vary.
How do you run a Python script directly after adding a shebang and making it executable?
Use the ./
prefix:$ ./hello.py
What output would you expect from running the script hello.py with the shebang and executable permissions?
The terminal would display:Hello, World!
How do you run a Python module using the -m
option?
Use the command:$ python -m <module-name>
This searches sys.path
for the module and runs its content.
What is required for running a module with python -m
?
The module must be in Python’s sys.path
.
Provide the module name without the .py
suffix.
Provide an example of running a Python file as a module.
$ python -m hello Hello, World!
What happens if the module specified with -m is not in sys.path?
Python raises an error:
No module named <module-name>
When is using the -m option particularly useful?
For running command-line interfaces (CLI) of standard library modules such as pip, venv, http.server, and zipfile.
What is the Python interpreter?
A software layer that runs Python code, working between your program and your computer hardware.
What are some implementations of the Python interpreter?
- CPython: Written in C, the core implementation of Python.
- PyPy: Written in Python, with a Just-In-Time (JIT) compiler for speed.
- Jython: Written in Java, integrates with the Java ecosystem.
- IronPython: Written for .NET, integrates with the .NET ecosystem.
What are the two modes in which the Python interpreter can run code?
- Script mode: Runs a Python file as an executable program.
- Interactive mode (REPL): Allows direct execution of typed code, with immediate feedback.
What is the REPL in Python?
REPL stands for Read-Eval-Print Loop, an interactive mode where you can type and run Python code directly in the interpreter.
Why is the REPL useful?
It’s helpful for learning Python, quickly testing code, debugging, and developing applications.
How do you start an interactive Python session?
Run the python
command without arguments:$ python
What does the Python interpreter prompt look like in Windows PowerShell?
PS> python Python 3.13.1 (main, Dec 5 2024, 14:38:34) [MSC v.1936 64 bit] on win32 >>>
What does the Python interpreter prompt look like in Linux?
$ python Python 3.13.1 (main, Dec 5 2024, 13:58:44) [GCC 9.4.0] on linux >>>
What does the Python interpreter prompt look like in macOS?
$ python Python 3.13.1 (main, Dec 3 2024, 19:03:43) [Clang 14.0.3] on darwin >>>
What is the primary prompt for the Python REPL?
The primary prompt consists of three right angle brackets: >>>
.
What is the secondary prompt in Python’s REPL, and when does it appear?
The secondary prompt consists of three periods: …, and it appears when adding indented lines to compound statements, such as conditionals, loops, and function or class definitions.
What is the Python interpreter also known as, and why?
It is also called the REPL (Read-Eval-Print Loop) because it:
- Reads your input (expressions and statements).
- Evaluates the code.
- Prints the output.
- Loops back to step one for continued interaction.
What is the primary advantage of using the Python REPL?
It allows you to write, run, and test Python code immediately, making it ideal for learning, experimenting, and debugging.
What happens to your code when you close a REPL session?
The code is lost because interactive sessions are non-persistent, unlike scripts.
Provide an example of code execution in a Python REPL session.
>>> print("Hello, World!") Hello, World! >>> 2 + 5 7 >>> print("Welcome to Real Python!") Welcome to Real Python!
How do you exit an interactive Python session?
- Use the built-in functions
quit()
orexit()
. - Press:
Ctrl+Z
and Enter on Windows.Ctrl+D
on Unix systems (Linux/macOS).
What happens when you import a module in Python?
Python loads the module’s content into memory, and any executable code in the module runs immediately during the first import.
What happens when a module contains only definitions (e.g., classes, functions, constants)?
The code runs during import, but you won’t notice it since definitions don’t produce visible output.
Provide an example of importing a module that runs executable code.
>>> import hello Hello, World!
Why does Python not execute code on successive imports of the same module?
To optimise performance, Python skips importing a module if it has already been imported in the same session.
How can you import a module programmatically in Python?
Use the importlib.import_module()
function from the importlib
module:
>>> import importlib >>> importlib.import_module("hello") Hello, World!
How do you force Python to reload an already imported module?
Use the importlib.reload()
function:
>>> import hello Hello, World! >>> import importlib >>> importlib.reload(hello) Hello, World!
What must you provide as an argument to the reload()
function?
A module object that has already been imported, not a string.
Why does Python optimise imports by skipping repeated imports?
Import operations are computationally expensive, and skipping repeated imports improves overall performance.
What is the purpose of Python’s built-in exec()
function?
It supports the dynamic execution of Python code, allowing you to run Python scripts directly from your code.
Provide an example of using exec() to run a script.
>>> with open("hello.py") as hello: ... exec(hello.read()) ... Hello, World!
What security risks are associated with using exec()?
Using exec() to run external code can expose your application to security vulnerabilities, as malicious code might be executed.
Why use an IDE or advanced text editor to run Python scripts?
IDEs and advanced text editors provide programmer-friendly features like debugging, syntax highlighting, and integrated run options, making them suitable for developing large and complex applications.
What is the default IDE included in Python’s standard distribution?
IDLE
How can you run a script in PyCharm?
Press Ctrl+R to run the app’s entry-point script.
How can you run a script in Visual Studio Code?
Press Ctrl+F5 to run the currently active file.
How can you run a Python script by double-clicking its icon in a file manager?
Double-clicking is possible if certain conditions are met, depending on the operating system.
What must be set up on Windows to run Python scripts by double-clicking?
The .py extension must be associated with python.exe.
The .pyw extension must be associated with pythonw.exe.
What requirements must be satisfied on Unix systems to run Python scripts by double-clicking?
- The script must have execution permissions.
- A shebang (e.g., #!/usr/bin/env python3) must be included in the script.
Why might you not see any output when running a CLI script by double-clicking?
File managers do not typically display output for scripts designed for the command line interface.
What are the limitations of running Python scripts by double-clicking?
- It depends on factors such as the operating system, file manager, execution permissions, and file associations.
- This method is less practical during development.
When is double-clicking to run a Python script most commonly used?
It is more commonly used for production-ready scripts and programs rather than during development.