Sub topic 15
Troubleshooting common setup problems
This chapter assumes you have installed Python 3, can open a terminal, and have run a script with python3 hello.py (or py hello.py on Windows). It gives you a method for diagnosing setup failures, then covers the most common ones.
A method for diagnosing problems
Most setup problems come down to one question: which program does the terminal actually run when you type python? Answer that first, and then read the error message.
Problem 1: "command not found" or "not recognized"
The terminal searches the folders listed in PATH for a program with the name you typed. If Python's folder is not on PATH, the search fails.
- On Windows, try
py --version. Thepylauncher is installed with Python and does not depend onPATH. If it works, you can use it for everything. Otherwise, rerun the installer and select Add python.exe to PATH. - On macOS and Linux, the command is usually
python3, notpython. Trypython3 --version. - After changing
PATHor installing, close the terminal and open a new one. Existing terminals keep the old value.
Problem 2: the wrong version runs
Several Pythons can be installed at once. Check which one answers:
python3 --version
which python3
# On Windows (Command Prompt): where python
# On Windows (PowerShell): Get-Command pythonIf the path is not the one you expect, another install appears earlier in PATH. Either call the interpreter by its full path, or use a virtual environment, which puts its own python first once activated.
Problem 3: Windows opens the Microsoft Store
On some Windows versions, typing python opens the Store because of an "app execution alias." Use py instead. You can also turn the alias off in Settings > Apps > Advanced app settings > App execution aliases.
Problem 4: `pip` installs to the wrong Python
A bare pip command may belong to a different interpreter than python. Always run pip through the interpreter you intend to use:
python -m pip install requests
python -m pip --versionThe second command prints the location of the pip it used, so you can confirm it matches your interpreter or active environment.
Problem 5: `ModuleNotFoundError`
If a script fails with ModuleNotFoundError: No module named 'requests', the package is missing from the interpreter running the script. The usual causes are:
- You installed the package in one environment and ran the script in another.
- The virtual environment is not activated. Your prompt normally shows
(.venv)when it is. - The name has a typo, or the import name differs from the install name.
Check with python -m pip list using the same python that runs the script.
Problem 6: PowerShell will not activate the environment
PowerShell may refuse to run Activate.ps1 because of its execution policy. Either use Command Prompt with .venv\Scripts\activate.bat, or allow local scripts for your user:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSignedProblem 7: the script cannot find its file
python3: can't open file ... No such file or directory means the terminal's current folder does not contain the script. Run pwd (or cd on Windows) to see where you are, and ls (or dir) to list files. Change folder with cd, or pass the full path to the script.
Also check the file name. Editors sometimes save hello.py as hello.py.txt, and Windows hides known extensions by default.
Summary table
| Symptom | Likely cause | First thing to try |
|---|---|---|
| `command not found` | Python not on `PATH`, or wrong command name | `python3` or `py`, then a new terminal |
| Wrong version prints | Another install is earlier on `PATH` | `which python3` or `where python` |
| Store opens on Windows | App execution alias | Use `py` |
| Package installed but not found | Different interpreter or environment | `python -m pip install` |
| Cannot open file | Wrong current folder or file name | `pwd` and `ls` |
Exercise
You run pip install requests and it succeeds, but your script fails with ModuleNotFoundError: No module named 'requests'. Give two commands that help find the cause, and say what each shows.
Show answer
Run python -m pip --version to see which interpreter's pip is used, and python -m pip list to see whether requests is installed for the same python that runs the script. A mismatch means the package went to a different interpreter or environment. Fix it by activating the right virtual environment and installing with python -m pip install requests.
Exercise
On Windows, python hello.py opens the Microsoft Store instead of running your script. What are two ways to fix it?
Show answer
Use the launcher with py hello.py, or turn off the Python app execution aliases in Settings under Apps > Advanced app settings > App execution aliases.