how to run python scripts Flashcards

1
Q

What is a script in computing?

A

A script is a text file containing a logical sequence of orders expressed in a scripting language, used to accomplish a specific task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a scripting language?

A

A programming language that allows you to manipulate, customise, and automate tasks, typically interpreted at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is Python often referred to as an interpreted language?

A

Python programs are executed by an interpreter at runtime rather than being compiled beforehand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why are Python programs often called scripts?

A

Because they are interpreted and can consist of sequential code, although Python programs can also be complex applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the key difference between a script and a module in Python?

A
  • Script: Contains executable code, typically run directly.
  • Module: Contains importable code designed to be used in other Python files.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is importable code?

A

Code that defines something, such as functions or classes, without performing specific actions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is executable code?

A

Code that performs specific actions, like function calls, loops, and conditionals.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an entry-point script?

A

A top-level Python program containing executable code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What file extension do Python scripts and modules typically use?

A

.py
.pyw extension used on Windows for Python applications that should use the pythonw.exe launcher.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create a Python script?

A

Use a Python-friendly code editor or IDE to write a program and save it with a .py extension.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you run a Python script using the python command on Windows PowerShell?

A
  1. Open PowerShell.
  2. Type python .\filename.py or py .\filename.py
  3. Press Enter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you run a Python script using the python command on Linux or macOS?

A
  1. Open the shell.
  2. Type python ./filename.py or python3 ./filename.py (if python3 is required).
  3. Press Enter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What should you do if the Python script doesn’t run from the command line?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the py command do on Windows?

A

Triggers the py.exe launcher for running Python console applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Provide an example of a basic Python script and its execution.

A

Script:

print("Hello, World!")  

Filename: hello.py
Execution:

Windows PowerShell: python .\hello.py
Linux/macOS shell: python ./hello.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is redirection in the context of terminal or shell applications?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you redirect the output of a Python script to a file?

A

Use the > symbol followed by the filename:
$ python hello.py > output.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What happens to the output file when using > for redirection?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you append the output of a Python script to the end of an existing file?

A

Use&raquo_space; instead of >:
$ python hello.py >> output.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the difference between > and&raquo_space; in shell redirection?

A

> : Overwrites the file’s content.
>: Appends the output to the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How would the file output.txt look after two consecutive executions of python hello.py >> output.txt?

A

It would contain the phrase “Hello, World!” twice, each on a new line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How can you run a Python script directly on Windows using its filename?

A

Enter the filename at the command prompt:
PS> .\hello.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Why does a new terminal window briefly appear when running Python scripts directly on Windows?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What happens when you try to run a Python script directly on Linux or macOS without preparation?

A

You get a “Permission denied” error due to Unix systems prioritising security.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you make a Python script executable on Linux or macOS?

A

Use the chmod command:
$ chmod +x hello.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Why might you encounter a “syntax error near unexpected token” error after making a script executable?

A

The operating system doesn’t know which interpreter to use and tries to execute the script with the shell instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How can you specify which interpreter should run your Python script on Unix systems?

A

Add a shebang line at the start of your script, e.g.:
#!/usr/bin/env python3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is a shebang in a script file?

A

A special comment at the beginning of a file that specifies which program should execute the script.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Provide two ways to specify the interpreter in a shebang.

A
  1. Absolute path to the interpreter: #!/usr/bin/python3
  2. Using the env command for portability: #!/usr/bin/env python3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Why is using #!/usr/bin/env python3 considered more portable than #!/usr/bin/python3?

A

The env command locates the Python interpreter dynamically, ensuring compatibility across Unix systems where the Python path may vary.

31
Q

How do you run a Python script directly after adding a shebang and making it executable?

A

Use the ./ prefix:
$ ./hello.py

32
Q

What output would you expect from running the script hello.py with the shebang and executable permissions?

A

The terminal would display:
Hello, World!

33
Q

How do you run a Python module using the -m option?

A

Use the command:
$ python -m <module-name>
This searches sys.path for the module and runs its content.

34
Q

What is required for running a module with python -m?

A

The module must be in Python’s sys.path.
Provide the module name without the .py suffix.

35
Q

Provide an example of running a Python file as a module.

A
$ python -m hello  
Hello, World!  
36
Q

What happens if the module specified with -m is not in sys.path?

A

Python raises an error:

No module named <module-name>

37
Q

When is using the -m option particularly useful?

A

For running command-line interfaces (CLI) of standard library modules such as pip, venv, http.server, and zipfile.

38
Q

What is the Python interpreter?

A

A software layer that runs Python code, working between your program and your computer hardware.

39
Q

What are some implementations of the Python interpreter?

A
  • 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.
40
Q

What are the two modes in which the Python interpreter can run code?

A
  • Script mode: Runs a Python file as an executable program.
  • Interactive mode (REPL): Allows direct execution of typed code, with immediate feedback.
41
Q

What is the REPL in Python?

A

REPL stands for Read-Eval-Print Loop, an interactive mode where you can type and run Python code directly in the interpreter.

42
Q

Why is the REPL useful?

A

It’s helpful for learning Python, quickly testing code, debugging, and developing applications.

43
Q

How do you start an interactive Python session?

A

Run the python command without arguments:
$ python

44
Q

What does the Python interpreter prompt look like in Windows PowerShell?

A
PS> python  
Python 3.13.1 (main, Dec 5 2024, 14:38:34) [MSC v.1936 64 bit] on win32  
>>>  
45
Q

What does the Python interpreter prompt look like in Linux?

A
$ python  
Python 3.13.1 (main, Dec 5 2024, 13:58:44) [GCC 9.4.0] on linux  
>>>  
46
Q

What does the Python interpreter prompt look like in macOS?

A
$ python  
Python 3.13.1 (main, Dec 3 2024, 19:03:43) [Clang 14.0.3] on darwin  
>>>  
47
Q

What is the primary prompt for the Python REPL?

A

The primary prompt consists of three right angle brackets: >>>.

48
Q

What is the secondary prompt in Python’s REPL, and when does it appear?

A

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.

49
Q

What is the Python interpreter also known as, and why?

A

It is also called the REPL (Read-Eval-Print Loop) because it:

  1. Reads your input (expressions and statements).
  2. Evaluates the code.
  3. Prints the output.
  4. Loops back to step one for continued interaction.
50
Q

What is the primary advantage of using the Python REPL?

A

It allows you to write, run, and test Python code immediately, making it ideal for learning, experimenting, and debugging.

51
Q

What happens to your code when you close a REPL session?

A

The code is lost because interactive sessions are non-persistent, unlike scripts.

52
Q

Provide an example of code execution in a Python REPL session.

A
>>> print("Hello, World!")  
Hello, World!  

>>> 2 + 5  
7  

>>> print("Welcome to Real Python!")  
Welcome to Real Python!  
53
Q

How do you exit an interactive Python session?

A
  1. Use the built-in functions quit() or exit().
  2. Press:
    Ctrl+Z and Enter on Windows.
    Ctrl+D on Unix systems (Linux/macOS).
54
Q

What happens when you import a module in Python?

A

Python loads the module’s content into memory, and any executable code in the module runs immediately during the first import.

55
Q

What happens when a module contains only definitions (e.g., classes, functions, constants)?

A

The code runs during import, but you won’t notice it since definitions don’t produce visible output.

56
Q

Provide an example of importing a module that runs executable code.

A
>>> import hello  
Hello, World!  
57
Q

Why does Python not execute code on successive imports of the same module?

A

To optimise performance, Python skips importing a module if it has already been imported in the same session.

58
Q

How can you import a module programmatically in Python?

A

Use the importlib.import_module() function from the importlib module:

>>> import importlib  
>>> importlib.import_module("hello")  
Hello, World!  
59
Q

How do you force Python to reload an already imported module?

A

Use the importlib.reload() function:

>>> import hello  
Hello, World!  

>>> import importlib  
>>> importlib.reload(hello)  
Hello, World!  
60
Q

What must you provide as an argument to the reload() function?

A

A module object that has already been imported, not a string.

61
Q

Why does Python optimise imports by skipping repeated imports?

A

Import operations are computationally expensive, and skipping repeated imports improves overall performance.

62
Q

What is the purpose of Python’s built-in exec() function?

A

It supports the dynamic execution of Python code, allowing you to run Python scripts directly from your code.

63
Q

Provide an example of using exec() to run a script.

A
>>> with open("hello.py") as hello:  
...     exec(hello.read())  
...  
Hello, World!  
64
Q

What security risks are associated with using exec()?

A

Using exec() to run external code can expose your application to security vulnerabilities, as malicious code might be executed.

65
Q

Why use an IDE or advanced text editor to run Python scripts?

A

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.

66
Q

What is the default IDE included in Python’s standard distribution?

A

IDLE

67
Q

How can you run a script in PyCharm?

A

Press Ctrl+R to run the app’s entry-point script.

68
Q

How can you run a script in Visual Studio Code?

A

Press Ctrl+F5 to run the currently active file.

69
Q

How can you run a Python script by double-clicking its icon in a file manager?

A

Double-clicking is possible if certain conditions are met, depending on the operating system.

70
Q

What must be set up on Windows to run Python scripts by double-clicking?

A

The .py extension must be associated with python.exe.
The .pyw extension must be associated with pythonw.exe.

71
Q

What requirements must be satisfied on Unix systems to run Python scripts by double-clicking?

A
  1. The script must have execution permissions.
  2. A shebang (e.g., #!/usr/bin/env python3) must be included in the script.
72
Q

Why might you not see any output when running a CLI script by double-clicking?

A

File managers do not typically display output for scripts designed for the command line interface.

73
Q

What are the limitations of running Python scripts by double-clicking?

A
  • It depends on factors such as the operating system, file manager, execution permissions, and file associations.
  • This method is less practical during development.
74
Q

When is double-clicking to run a Python script most commonly used?

A

It is more commonly used for production-ready scripts and programs rather than during development.