KAHIBARO Bookshelves
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:

  • hello.py
  • game.py
  • my_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.

  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:

  • Windows (PowerShell or Command Prompt):
  cd C:\Users\YourName\python_projects
  • macOS / Linux (Terminal):
  cd /Users/YourName/python_projects
  # or
  cd /home/YourName/python_projects

You 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.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):

  • Windows:
  python C:\Users\YourName\python_projects\hello.py
  • macOS / Linux:
  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 File → New File.
  3. Type your code:
   print("Running from IDLE!")
  1. Save the file as idle_test.py.
  2. Run it with:
    • Run → Run 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:

  • Make sure the terminal’s current directory is your project folder.
  • Run:
  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:

  • 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 command

it usually means:

  • Python is not installed, or
  • The python command 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 directory

Possible causes:

  • You are not in the correct folder (use cd to move to the folder with your script).
  • The filename is slightly different (e.g., Hello.py vs hello.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:

  • python might refer to Python 2 (older version).
  • python3 refers to Python 3.

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:

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

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Chapter 1: What Is Programming?
    • ☰ 1.1. What is a computer program?
      • ☰ 1.2. What is Python?
        • ☰ 1.3. Where is Python used?
          • ☰ 1.4. How programming helps solve problems
            • ☰ 1.5. Your first Python program
            • ☰ 2. Chapter 2: Setting Up Python
              • ☰ 2.1. Installing Python (Windows / macOS / Linux)
                • ☰ 2.2. Using Python in the terminal
                  • ☰ 2.3. Using an IDE (IDLE / VS Code)
                    • ☰ 2.4. Running Python scripts
                      • ☰ 2.5. Python interactive mode
                      • ☰ 3. Chapter 3: Variables and Data Types
                        • ☰ 3.1. What is a variable?
                          • ☰ 3.2. Creating variables
                            • ☰ 3.3. Numbers
                              • ☰ 3.4. Test
                                • ☰ 3.5. Boolean values
                                  • ☰ 3.6. Type checking
                                  • ☰ 4. Chapter 4: Basic Operations
                                    • ☰ 4.1. Arithmetic operations
                                      • ☰ 4.2. Working with numbers
                                        • ☰ 4.3. String operations
                                          • ☰ 4.4. Combining text and variables
                                            • ☰ 4.5. Comments in Python
                                            • ☰ 5. Chapter 5: User Input and Output
                                              • ☰ 5.1. Getting input from the user
                                                • ☰ 5.2. Converting input to numbers
                                                  • ☰ 5.3. Formatting output
                                                    • ☰ 5.4. Writing interactive programs
                                                    • ☰ 6. Chapter 6: Conditions
                                                      • ☰ 6.1. Comparison operators
                                                        • ☰ 6.2. Logical operators
                                                          • ☰ 6.3. if, elif, else
                                                            • ☰ 6.4. Nested conditions
                                                              • ☰ 6.5. Common beginner mistakes
                                                              • ☰ 7. Chapter 7: Loops
                                                                • ☰ 7.1. Why loops are useful
                                                                  • ☰ 7.2. for loops
                                                                    • ☰ 7.3. while loops
                                                                      • ☰ 7.4. Loop counters
                                                                        • ☰ 7.5. break and continue
                                                                          • ☰ 7.6. Loop exercises
                                                                          • ☰ 8. Chapter 8: Data Collections
                                                                            • ☰ 8.1. Lists
                                                                              • ☰ 8.2. Tuples
                                                                                • ☰ 8.3. Dictionaries
                                                                                  • ☰ 8.4. Sets (basics)
                                                                                    • ☰ 8.5. Choosing the right data structure
                                                                                    • ☰ 9. Chapter 9: Functions
                                                                                      • ☰ 9.1. Why functions matter
                                                                                        • ☰ 9.2. Defining functions
                                                                                          • ☰ 9.3. Parameters and arguments
                                                                                            • ☰ 9.4. Return values
                                                                                              • ☰ 9.5. Scope (local vs global)
                                                                                                • ☰ 9.6. Reusing code
                                                                                                • ☰ 10. Chapter 10: Working with Files
                                                                                                  • ☰ 10.1. What is a file?
                                                                                                    • ☰ 10.2. Reading text files
                                                                                                      • ☰ 10.3. Writing and appending files
                                                                                                        • ☰ 10.4. File paths
                                                                                                          • ☰ 10.5. Simple file-based programs
                                                                                                          • ☰ 11. Chapter 11: Errors and Debugging
                                                                                                            • ☰ 11.1. Types of errors
                                                                                                              • ☰ 11.2. Reading error messages
                                                                                                                • ☰ 11.3. Common beginner errors
                                                                                                                  • ☰ 11.4. try and except
                                                                                                                    • ☰ 11.5. Debugging strategies
                                                                                                                    • ☰ 12. Chapter 12: Using Python Libraries
                                                                                                                      • ☰ 12.1. What is a library?
                                                                                                                        • ☰ 12.2. Importing modules
                                                                                                                          • ☰ 12.3. Standard libraries
                                                                                                                            • ☰ 12.3.1. math
                                                                                                                              • ☰ 12.3.2. random
                                                                                                                                • ☰ 12.3.3. datetime
                                                                                                                                • ☰ 12.4. Installing external libraries with pip
                                                                                                                                • ☰ 13. Chapter 13: Object-Oriented Programming
                                                                                                                                  • ☰ 13.1. What is object-oriented programming?
                                                                                                                                    • ☰ 13.2. Classes and objects
                                                                                                                                      • ☰ 13.3. The `__init__` method
                                                                                                                                        • ☰ 13.4. Attributes and methods
                                                                                                                                          • ☰ 13.5. Encapsulation
                                                                                                                                            • ☰ 13.6. Inheritance (basics)
                                                                                                                                              • ☰ 13.7. Simple OOP examples
                                                                                                                                              • ☰ 14. Chapter 14: Automation with Python
                                                                                                                                                • ☰ 14.1. What is automation?
                                                                                                                                                  • ☰ 14.2. Automating simple tasks
                                                                                                                                                    • ☰ 14.3. Working with files and folders
                                                                                                                                                      • ☰ 14.4. Automating text processing
                                                                                                                                                        • ☰ 14.5. Scheduling scripts
                                                                                                                                                          • ☰ 14.6. Practical automation examples
                                                                                                                                                          • ☰ 15. Chapter 15: Web Development with Python
                                                                                                                                                            • ☰ 15.1. How the web works (basic overview)
                                                                                                                                                              • ☰ 15.2. Introduction to Flask
                                                                                                                                                                • ☰ 15.3. Creating a simple web server
                                                                                                                                                                  • ☰ 15.4. Routing and templates
                                                                                                                                                                    • ☰ 15.5. Handling user input (forms)
                                                                                                                                                                      • ☰ 15.6. Very basic APIs
                                                                                                                                                                      • ☰ 16. Chapter 16: Data Science with Python
                                                                                                                                                                        • ☰ 16.1. What is data science?
                                                                                                                                                                          • ☰ 16.2. Working with data
                                                                                                                                                                            • ☰ 16.3. Introduction to NumPy
                                                                                                                                                                              • ☰ 16.4. Introduction to pandas
                                                                                                                                                                                • ☰ 16.5. Data visualization with matplotlib
                                                                                                                                                                                  • ☰ 16.6. Simple data analysis examples
                                                                                                                                                                                  • ☰ 17. Chapter 17: Mini Projects
                                                                                                                                                                                    • ☰ 17.1. Number guessing game
                                                                                                                                                                                      • ☰ 17.2. Simple calculator
                                                                                                                                                                                        • ☰ 17.3. To-do list
                                                                                                                                                                                          • ☰ 17.4. Password generator
                                                                                                                                                                                            • ☰ 17.5. Quiz application
                                                                                                                                                                                              • ☰ 17.6. Small web or data project
                                                                                                                                                                                              • ☰ 18. Chapter 18: Next Steps
                                                                                                                                                                                                • ☰ 18.1. How to practice Python
                                                                                                                                                                                                  • ☰ 18.2. Writing clean and readable code
                                                                                                                                                                                                    • ☰ 18.3. Choosing a specialization
                                                                                                                                                                                                      • ☰ 18.3.1. Web development
                                                                                                                                                                                                        • ☰ 18.3.2. Data science
                                                                                                                                                                                                          • ☰ 18.3.3. Automation
                                                                                                                                                                                                            • ☰ 18.3.4. Software development
                                                                                                                                                                                                            • ☰ 18.4. Learning resources and communities
                                                                                                                                                                                                            • ☰ 19. Appendix
                                                                                                                                                                                                              • ☰ 19.1. Python syntax cheat sheet
                                                                                                                                                                                                                • ☰ 19.2. Common errors and solutions
                                                                                                                                                                                                                  • ☰ 19.3. Glossary of terms