Table of Contents
What Is a “Python Script”?
So far, you might have run small bits of Python code in an interactive prompt or IDE. A Python script is simply a text file that contains Python code and is saved with the extension .py, for example:
hello.pygame.pymy_script.py
Instead of typing commands one by one, you put them all in this .py file and run the whole file at once.
Typical script contents:
print("Hello from a script!")
name = "Alex"
print("Nice to meet you,", name)When you “run a script”, Python executes everything in the file from top to bottom.
Creating Your First Script File
You can create a Python script with any plain text editor or IDE. The exact editor details live in the IDE chapter, so here we’ll focus on what the file itself looks like and how to run it.
- Open your editor (IDLE, VS Code, or any other code editor).
- Create a new file.
- Type some simple Python code:
# hello.py
print("Hello, Python script!")- Save the file as
hello.pyin a folder you can easily find, such as: - Windows:
C:\Users\YourName\python_projects\hello.py - macOS/Linux:
/Users/YourName/python_projects/hello.pyor/home/YourName/python_projects/hello.py
Running a Script from the Terminal
You should already know how to open a terminal and use Python in it from the earlier chapter about using Python in the terminal. Here we focus on using the terminal to run script files.
1. Find Your Script’s Folder
In the terminal, change directory (cd) to where your script is saved.
Example: If hello.py is in python_projects in your home directory:
- Windows (PowerShell or Command Prompt):
cd C:\Users\YourName\python_projects- macOS / Linux (Terminal):
cd /Users/YourName/python_projects
# or
cd /home/YourName/python_projectsYou can usually list files in the current directory with:
- Windows:
dir - macOS/Linux:
ls
Check that you can see hello.py in the list.
2. Run the Script
The general pattern is:
- On many systems:
python your_script.py - On some systems (especially macOS/Linux):
python3 your_script.py
Try:
- Windows (Command Prompt or PowerShell):
python hello.py- macOS / Linux:
python3 hello.pyIf everything is set up correctly, you should see:
Hello, Python script!
If python doesn’t work but python3 does (or vice versa), just use the one that works consistently on your machine.
3. Running Scripts from Anywhere (Using Paths)
You don’t always have to be inside the script’s folder to run it. You can also provide the full path to the script.
Example (replace with your actual username/path):
- Windows:
python C:\Users\YourName\python_projects\hello.py- macOS / Linux:
python3 /Users/YourName/python_projects/hello.pyBe careful with spaces in folder names. If your path has spaces, wrap it in quotes, e.g.:
python "C:\Users\Your Name\My Projects\hello.py"Running Scripts from IDLE
If you use IDLE (Python’s simple built-in editor), you can run scripts without touching the terminal.
- Open IDLE.
- Go to
File→New File. - Type your code:
print("Running from IDLE!")- Save the file as
idle_test.py. - Run it with:
Run→Run Module(or pressF5).
A new window (the Python Shell) will show the output:
Running from IDLE!IDLE always runs the currently open script file when you choose “Run Module”.
Running Scripts in VS Code
If you use VS Code, there are several ways to run a script. The exact setup may differ slightly depending on your Python extension and configuration, but the pattern is similar.
Basic approach:
- Open your project folder in VS Code.
- Open
hello.pyin the editor. - Look for a Run button (often a green triangle) at the top right of the editor.
- Click it to run the file. Output will appear in the “Terminal” or “Python” output pane.
Alternatively, if you have a terminal open inside VS Code:
- Make sure the terminal’s current directory is your project folder.
- Run:
python hello.py
# or
python3 hello.pyThis is the same as running in an external terminal, just integrated into the editor.
Scripts vs. Interactive Mode
Running a script is different from typing directly into the interactive Python prompt.
Interactive mode:
- You type one line at a time.
- Python executes each line immediately.
- Great for quick tests.
Script mode (running a .py file):
- You save multiple lines of code in a file.
- Python executes the entire file from top to bottom when you run it.
- Better for real programs, re-running experiments, and sharing code.
You can often start in interactive mode to experiment, then copy working code into a script file.
Command-Line Arguments (Preview)
When running a script from the terminal, you can also give it extra information, called command-line arguments. This is just a preview; details of processing input belong in later chapters.
Example command:
python my_script.py input.txt output.txt
Here, input.txt and output.txt are arguments. Inside Python, you can access them using modules from the standard library (covered later). For now, just be aware that:
- You can pass extra values after the script name.
- This makes scripts more flexible and configurable.
Common Problems When Running Scripts
Beginners often hit the same few issues when trying to run Python scripts. Here are quick pointers so you can recognize them.
1. “python is not recognized” (Windows)
If you see something like:
'python' is not recognized as an internal or external commandit usually means:
- Python is not installed, or
- The
pythoncommand is not on your system’s PATH.
Try:
py hello.py(on some Windows setups).- Or
python3 hello.py. - Or revisit the Python installation chapter to ensure “Add Python to PATH” was checked.
2. “No such file or directory”
If you see:
python: can't open file 'hello.py': [Errno 2] No such file or directoryPossible causes:
- You are not in the correct folder (use
cdto move to the folder with your script). - The filename is slightly different (e.g.,
Hello.pyvshello.py). - On macOS/Linux, remember that filenames are case-sensitive.
Check where your file is saved and that you spelled the name exactly.
3. Using the Wrong Python Command
On some systems:
pythonmight refer to Python 2 (older version).python3refers to Python 3.
If one doesn’t work or gives unexpected results, try the other:
python3 hello.pyYour system’s setup chapter explains how Python was installed; use whichever command matches your installation.
When to Run a Script vs. Use Interactive Mode
Use interactive mode when:
- You’re quickly checking how something works.
- You want to test a small expression or function call.
Use a script when:
- Your code has multiple lines you want to reuse.
- You’re building a program you’ll run many times.
- You want to share your code with others.
- You want to keep your work organized in files.
Running scripts is a core habit in real Python development: almost every project is made up of script files (and packages) you run from an editor or terminal.