7 Fixes for When Python Won't Output Anything (2025)
Staring at an error instead of your running Python script? Don't panic. Here are 7 common, easy-to-fix reasons why Python won't run and how to solve them.
Marco Diaz
A senior Python developer and technical writer passionate about making coding accessible.
7 Reasons Your Python Script Won't Run (And How to Fix Them)
We’ve all been there. You’ve written a brilliant piece of Python code. You navigate to your terminal, type python my_awesome_script.py
with a flourish, hit Enter, and... nothing. Or worse, a cryptic error message that sends you straight to Google.
It’s a universal experience for programmers, from absolute beginners to seasoned developers. When Python decides to take an unscheduled break, it can be incredibly frustrating. But don't worry! The problem is almost always one of a handful of common, easy-to-fix issues.
Before you start questioning your life choices, let's walk through the seven most frequent culprits for a non-running Python script. We'll get you back to coding in no time.
1. The Infamous PATH Problem
This is, without a doubt, the number one issue for newcomers. You've installed Python, but when you type python
in your command prompt or terminal, you get an error like 'python' is not recognized...
(Windows) or command not found: python
(macOS/Linux).
The Problem: Your computer's command line doesn't know where to find the Python executable. The system's PATH is a list of directories where it looks for programs. If the directory containing `python.exe` isn't on that list, your shell has no idea what you're talking about.
The Fix:
- On Windows: The easiest solution is to re-run the Python installer. On the very first screen, make sure you check the box that says "Add Python X.X to PATH." It's easy to miss, but it's crucial.
- On macOS/Linux: This is less common with modern installers (like Homebrew), but if it happens, you'll need to manually add the Python installation's `bin` directory to your shell's configuration file (e.g., `.zshrc`, `.bash_profile`, or `.bashrc`).
You can check if Python is accessible by running:
python --version
If you see a version number, your PATH is set up correctly!
2. The `python` vs. `python3` Mix-up
You run your script, and it immediately crashes with a weird `SyntaxError` on a line of code you know is correct, like an f-string. What gives?
The Problem: Many systems, especially macOS and Linux, come with an older version of Python (Python 2) pre-installed and linked to the python
command. When you install Python 3, it often gets linked to the python3
command. Your script uses modern Python 3 features, but you're accidentally running it with Python 2.
The Fix:
- Be Explicit: Get into the habit of always using
python3
to run your scripts. - Check Your Versions: See what each command points to.
- Use the Python Launcher for Windows: Windows users have a handy tool called the Python Launcher. You can use
py my_script.py
and it will automatically find the latest version of Python you have installed. You can even specify a version withpy -3.9 my_script.py
.
python3 my_awesome_script.py
python --version
python3 --version
3. Missing Modules: `ModuleNotFoundError`
You try to run your script and are immediately met with the dreaded ModuleNotFoundError: No module named 'pandas'
. You're sure you installed it!
The Problem: Your script depends on a third-party library (like `requests`, `numpy`, or `pandas`), but that library isn't installed in the specific Python environment you're using.
The Fix: This one is usually straightforward. Install the missing module using `pip`, Python's package installer.
# For the 'pandas' example
pip install pandas
# Or if you're using python3 explicitly
python3 -m pip install pandas
Pro Tip: If you're sharing your project with others, create a `requirements.txt` file that lists all the dependencies. You can generate it with `pip freeze > requirements.txt`. Then, anyone (including future you) can install everything needed with one command: `pip install -r requirements.txt`.
4. Your Virtual Environment Isn't Activated
This is the sneaky cousin of the `ModuleNotFoundError`. You know you installed the packages. You can see them in your project's `venv` folder. But Python still can't find them.
The Problem: You created a virtual environment (which is a fantastic best practice!) to keep your project's dependencies isolated, but you forgot to activate it in your current terminal session. When the environment isn't active, your terminal defaults to the global Python installation, which doesn't have your project-specific packages.
The Fix: Activate the virtual environment before you do anything else. You need to do this every time you open a new terminal.
- On macOS/Linux:
source venv/bin/activate
.\venv\Scripts\activate
You'll know it's working when you see the name of your environment in parentheses at the start of your command prompt, like `(venv) C:\Users\...>`.
5. Simple Syntax and Indentation Errors
Sometimes, Python won't even try to run. It just gives you a `SyntaxError` or `IndentationError` and points to a line of code.
The Problem: A simple typo. Python is very strict about its grammar and structure. A missing colon, an unclosed parenthesis, or—most famously—incorrect indentation will stop the interpreter in its tracks.
SyntaxError: invalid syntax
: Often caused by a missing:
at the end of a `def`, `if`, `for`, or `while` line.IndentationError: expected an indented block
: The code following a colon must be indented.
The Fix: This is a rite of passage. The only fix is to carefully read the error message—it usually tells you exactly where the problem is—and correct the code. Using a modern code editor like VS Code with a Python extension is a lifesaver here. It will highlight these errors before you even try to run the script.
6. You're in the Wrong Directory
You type `python my_script.py` and get python: can't open file 'my_script.py': [Errno 2] No such file or directory
.
The Problem: Your terminal's current working directory is not the same directory where your Python file is saved. You're telling Python to run a file that doesn't exist *in your current location*.
The Fix:
- Check where you are: Use the `pwd` (print working directory) command on macOS/Linux or `cd` (current directory, with no arguments) on Windows.
- List the files: Use `ls` (macOS/Linux) or `dir` (Windows) to see the files in your current directory. Is your script there?
- Navigate to the correct directory: Use the `cd` (change directory) command to move to the folder containing your script. For example:
- For making a script executable (Linux/macOS): If you're trying to run your script directly like `./my_script.py` (after adding a shebang line like `#!/usr/bin/env python3`), you need to give it execute permissions.
- For file access issues: If your script is failing on a line like `open('data.csv', 'r')`, you need to check the permissions of `data.csv`. Ensure your user has at least read permission for that file.
cd Documents/MyPythonProject
Once you're in the right place, run the command again. It should work perfectly.
7. Sneaky File Permission Issues
This is less common for simple scripts but can pop up, especially on Linux or macOS. You try to run a script and get a `Permission denied` error.
The Problem: Your user account doesn't have the necessary permissions to either (a) execute the script file itself, or (b) read or write a file that your script is trying to access (like a data file).
The Fix:
chmod +x my_script.py
Conclusion: Don't Let It Stop You
Running into errors is a normal, everyday part of programming. It doesn't mean you're a bad coder; it just means you're a coder. The key is to learn to read the error messages and develop a mental checklist of likely suspects.
Next time Python refuses to cooperate, take a deep breath and run through these seven checks. Chances are, your problem is on this list, and the solution is just a few keystrokes away.
Happy coding!