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