Sub topic 15
Troubleshooting common setup problems
This chapter assumes you have read the earlier chapters on installing Python, PATH and the launcher commands, running scripts, tracebacks, and virtual environments. Here you will turn that knowledge into a diagnostic routine. Most setup problems come from one of four causes: the shell cannot find the interpreter, the shell finds the wrong interpreter, the interpreter is right but the packages are installed somewhere else, or the script has a mistake that the traceback already describes.
A diagnostic routine
Guessing wastes time. Ask the same questions in the same order each time.
- Does the shell find any interpreter? Run
python3 --version, orpy --versionon Windows. - Which file is being run? Ask the shell for the path.
- Which interpreter is running inside Python? Print
sys.executable. - Is a virtual environment active? Check the prompt prefix and
sys.prefix. - If the problem is an error message, read the last line of the traceback first, then work upward.
Command not found
Typical messages:
- macOS and Linux:
bash: python3: command not foundorzsh: command not found: python - Windows:
'python' is not recognized as an internal or external command
The message means the shell searched each folder listed in PATH and found no executable with that name. There are three usual reasons.
- Python is not installed. Install it as described in the installation chapter.
- Python is installed, but its folder is not in PATH. On Windows this happens when the installer option to add Python to PATH was left unchecked.
- You opened the terminal before installing. A terminal reads PATH when it starts, so close it and open a new one.
On Linux and macOS, the name matters. Many systems provide python3 but not python. Use python3 unless you have a reason not to. Inside an activated virtual environment, python always works because the environment provides it.
On Windows, try the py launcher, which does not depend on PATH in the same way:
py --version
py -0The second command lists every Python version the launcher can find. If py works and python does not, you can keep using py, or add Python to PATH.
Windows: the Microsoft Store alias
On some Windows installations, typing python opens the Microsoft Store instead of running Python. Windows ships small placeholder programs named python.exe and python3.exe that redirect to the Store. They can appear in PATH ahead of your real installation.
To fix it, open Settings, search for "Manage app execution aliases", and turn off the aliases for Python. Then open a new terminal.
The wrong Python runs
Sometimes python3 --version succeeds but shows a version you did not expect, such as an old release. Several interpreters can be installed at once, and the shell picks the first match in PATH order. Find out which one it picked:
which -a python3
# On Windows (Command Prompt): where python
# On Windows (PowerShell): Get-Command python -AllThe first line is the one that runs. To confirm from inside Python, run this one-line check:
python3 -c "import sys; print(sys.executable); print(sys.version)"sys.executable is the full path to the running interpreter, and it is the most reliable answer to "which Python is this?". If it points to a system location such as /usr/bin/python3 when you wanted your own installation, you have two options.
- Call the version you want by its full name, for example
python3.12, orpy -3.12on Windows. - Move the folder you want earlier in PATH.
Do not delete or replace the Python that your operating system provides. Linux distributions and older macOS releases use it for their own tools, and removing it can break them.
Pip installs into the wrong place
A very common symptom looks like this: pip install requests reports success, but running your script gives ModuleNotFoundError: No module named 'requests'. The pip command on your PATH belongs to a different interpreter than the python that runs your script.
The fix is to run pip through the interpreter you intend to use:
python3 -m pip install requests
python3 -m pip --versionpip --version shows the interpreter's site-packages folder that pip is attached to. If that folder does not match your interpreter, the two are out of step. The python -m pip form removes the guesswork because it always uses the pip belonging to that exact interpreter.
You can also check from the other side. Run this script, saved as where_is_it.py:
import importlib.util
import sys
print("Interpreter:", sys.executable)
spec = importlib.util.find_spec("requests")
print("requests found at:", spec.origin if spec else "not installed")If it prints not installed, the package is not visible to this interpreter. Install it with python -m pip install requests using the same command you use to run the script.
Virtual environment mistakes
Virtual environments cause their own set of problems.
- Not activated. The prompt has no
(.venv)prefix. Packages then install into, or are read from, the global interpreter. Activate the environment again, or call.venv/bin/pythondirectly (.venv\Scripts\python.exeon Windows). - Activated in a different terminal. Activation applies only to the terminal window where you ran it. A new window or an editor's built-in terminal may not have it.
- Editor uses another interpreter. Editors such as VS Code keep their own setting for which interpreter to use. If the terminal works but the editor's run button fails, select the
.venvinterpreter in the editor. - Moved or renamed folder. A virtual environment stores absolute paths. If you move the project folder, the environment breaks. Delete
.venvand create it again, then reinstall packages from a requirements file.
PowerShell refuses to activate the environment
On Windows PowerShell, running .venv\Scripts\Activate.ps1 may fail with a message saying that running scripts is disabled on this system. This is the PowerShell execution policy, which blocks local script files by default on some systems.
Two ways forward:
- Use Command Prompt and run
.venv\Scripts\activate.batinstead. - Allow local scripts for your user only:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned.
Skipping activation altogether also works. Calling .venv\Scripts\python.exe -m pip install requests installs into the environment without any activation.
Errors that look like setup problems
Some failures are not about installation. The traceback tells you which kind you have. Read the last line first.
| Last line of the traceback | Meaning | Usual fix |
|---|---|---|
| `ModuleNotFoundError: No module named 'x'` | The interpreter cannot import `x` | Install `x` with `python -m pip` in the active environment, or fix a typo |
| `NameError: name 'mesage' is not defined` | A variable or function name is misspelled or not yet defined | Check spelling and order of statements |
| `SyntaxError: invalid syntax` | Python cannot parse the line shown | Check brackets, quotes, colons, and the line above |
| `IndentationError` | Mixed or inconsistent indentation | Use four spaces everywhere; set the editor to insert spaces for tabs |
| `FileNotFoundError: ... 'hello.py'` | The path is wrong, or the shell is in a different folder | Run `pwd` (or `cd` on Windows) and change to the script's folder |
| `PermissionError` | You lack rights on that file or folder | Use a folder you own; avoid system folders |
A related trap is naming your own file after a module. If you save a script as random.py or requests.py, then import random or import requests loads your file instead of the real module. The error is confusing, such as AttributeError: module 'random' has no attribute 'randint'. Rename the file and delete the __pycache__ folder beside it.
Smaller problems worth recognizing
- Smart quotes. Text pasted from a web page or document may contain curly quotes. Python reports
SyntaxError: invalid character. Retype the quotes in the editor. - Running Python code in the shell. Typing
print("hi")at the shell prompt produces a shell error, not a Python error. Startpython3first, or put the code in a file. - Typing shell commands in the REPL. The opposite mistake: typing
python3 hello.pyat the>>>prompt gives aSyntaxError. Leave the REPL withexit()or Ctrl+D (Ctrl+Z then Enter on Windows). - The prompt does not return. If the terminal appears frozen, the program may be waiting for input or running a loop. Press Ctrl+C to interrupt it. You will see a
KeyboardInterrupttraceback, which is normal. - Script runs in the editor but not the terminal. The editor and terminal are using different interpreters or working folders. Compare
sys.executableandos.getcwd()in both.
A reusable report
When you need to ask for help, collect facts first. This script prints the details that others need to diagnose a setup problem:
import os
import platform
import sys
print("Python version:", platform.python_version())
print("Executable: ", sys.executable)
print("Prefix: ", sys.prefix)
print("In a venv: ", sys.prefix != sys.base_prefix)
print("Platform: ", platform.platform())
print("Working dir: ", os.getcwd())Save it as diagnose.py and run it with the same command that fails for you. Include its output with the full traceback, copied as text rather than as a screenshot.
Exercise
You run pip install requests and see "Successfully installed". Your script then fails with ModuleNotFoundError: No module named 'requests'. Name two commands you would run to find the cause, and the command you would use to install the package correctly.
Show answer
Run python3 -c "import sys; print(sys.executable)" to see which interpreter runs your script, and pip --version (or python3 -m pip --version) to see which interpreter pip is attached to. If the paths differ, pip installed into another interpreter. Install with python3 -m pip install requests, using the same interpreter (and the same activated virtual environment) that runs the script.
Exercise
On Windows, typing python opens the Microsoft Store. What is happening, and how do you fix it?
Show answer
Windows has placeholder python.exe aliases that redirect to the Store, and they come before your real installation in PATH. Turn them off under Settings, "Manage app execution aliases", then open a new terminal. You can also use the py launcher in the meantime.
Exercise
You save a script as random.py and it fails with AttributeError: module 'random' has no attribute 'randint' even though you never defined randint. Why, and what do you do?
Show answer
import random loaded your own random.py from the current folder instead of Python's standard library module. Rename your file to something that does not match a module name, and delete the __pycache__ folder next to it.