Kahibaro
Discord Login Register

2.4 Running Python scripts

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:

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.

  1. Open your editor (IDLE, VS Code, or any other code editor).
  2. Create a new file.
  3. Type some simple Python code:
   # hello.py
   print("Hello, Python script!")
  1. Save the file as hello.py in a folder you can easily find, such as:
    • Windows: C:\Users\YourName\python_projects\hello.py
    • macOS/Linux: /Users/YourName/python_projects/hello.py or /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:

  cd C:\Users\YourName\python_projects
  cd /Users/YourName/python_projects
  # or
  cd /home/YourName/python_projects

You can usually list files in the current directory with:

Check that you can see hello.py in the list.

2. Run the Script

The general pattern is:

Try:

  python hello.py
  python3 hello.py

If 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):

  python C:\Users\YourName\python_projects\hello.py
  python3 /Users/YourName/python_projects/hello.py

Be 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.

  1. Open IDLE.
  2. Go to FileNew File.
  3. Type your code:
   print("Running from IDLE!")
  1. Save the file as idle_test.py.
  2. Run it with:
    • RunRun Module (or press F5).

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:

  1. Open your project folder in VS Code.
  2. Open hello.py in the editor.
  3. Look for a Run button (often a green triangle) at the top right of the editor.
  4. 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:

  python hello.py
  # or
  python3 hello.py

This 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:

Script mode (running a .py file):

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:

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 command

it usually means:

Try:

2. “No such file or directory”

If you see:

python: can't open file 'hello.py': [Errno 2] No such file or directory

Possible causes:

Check where your file is saved and that you spelled the name exactly.

3. Using the Wrong Python Command

On some systems:

If one doesn’t work or gives unexpected results, try the other:

python3 hello.py

Your 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:

Use a script when:

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.

Views: 79

Comments

Please login to add a comment.

Don't have an account? Register now!