Python ImportError after pip install? Here's the real fix
You installed a package with pip, but Python still can't find it. I'll show you why that happens and how to fix it in 30 seconds.
I know this error is infuriating
You ran pip install requests, saw the success message, typed import requests, and got slapped with ImportError: No module named 'requests'. It makes you want to throw your laptop across the room. I've been there. Let's fix it.
The quick fix (90% of cases)
You're almost certainly using a different Python interpreter than the one pip installed to. Here's the command that solves this in under 15 seconds:
python -m pip install requestsIf you already ran pip install requests, run this instead:
python -m pip install --upgrade --force-reinstall requestsThen try your import again. If it still fails, use this to check what Python you're actually running:
which python # on macOS/Linux
where python # on Windows Command Prompt
Get-Command python | Select-Object Source # on PowerShellThen do the same for pip:
which pip
pip --versionLook at the version output — you'll see something like pip 23.0 from /usr/local/lib/python3.11/site-packages/pip (python 3.11). Match it against your Python version. That mismatch is your problem.
Why this happens
Python's import system looks in a list of directories called sys.path. When you run pip install requests, pip puts the package into the site-packages directory belonging to the Python interpreter that pip is tied to. If your system has Python 3.9 and Python 3.11 side by side (very common), pip might install to 3.11's site-packages while your python command runs 3.9. Your import fails because the package literally doesn't exist where Python is looking.
This is especially common on macOS (with its built-in Python 2 legacy) and Ubuntu (which ships Python 3 but also installs a second Python via apt). Windows users hit this when they install Python from the Microsoft Store and also from python.org — you end up with two different Pythons and pip talks to the wrong one.
Less common variations of this issue
1. You're inside a virtual environment but pip installed globally
If you activated a virtual environment with source venv/bin/activate but then ran pip install from a different terminal window, pip installed to the global site-packages, not your venv. Quick check:
which pip # should point inside your venv folder, like /project/venv/bin/pipIf it doesn't, deactivate and reactivate from the correct terminal.
2. The package name doesn't match the module name
This one trips up everyone at least once. The pip package name is beautifulsoup4, but you import bs4. Or Pillow installs as PIL. Or python-dotenv becomes dotenv. Check the package's documentation — sometimes the import name is completely different from the pip name. Run this to see what got installed:
pip show package_nameLook at the Location field — that's the directory where pip put the files. Navigate there and you'll see the actual folder names you can import.
3. You used sudo (Linux/macOS) and broke permissions
Running sudo pip install is generally a bad idea — it can change ownership of pip's internal files. If you accidentally did that, your user account's Python might not have permission to read the package files. Fix it by reinstalling without sudo:
pip install --user requestsOr better, use a virtual environment so you never need sudo.
4. IDE or editor uses a different Python
VS Code, PyCharm, and even some terminals can use a different Python than what you think. In VS Code, check the bottom-left corner — you'll see something like Python 3.11.4. Click it and select the interpreter that matches where you installed the package. In PyCharm, go to Settings > Project > Python Interpreter and make sure the path matches.
How to prevent this for good
Stop relying on the global Python. Here's my setup that hasn't failed me in three years:
- Always use a virtual environment. One per project. Create it with
python -m venv venv(Python 3.3+) orvirtualenv venvfor older Python 2 projects. - Activate before installing. Every time. No exceptions.
- Use
python -m pipinstead ofpip. This ensures you're installing with the exact Python you'll be using to run the script. Make it a muscle memory. - Pin your Python version. If your project uses Python 3.11, don't install packages with Python 3.12's pip. Use
pyenv(macOS/Linux) orpy -3.11(Windows) to stay consistent. - Use a requirements.txt file. Run
pip freeze > requirements.txtafter installing everything. Then on a new machine,python -m pip install -r requirements.txtbrings in the exact same packages.
That's it. Next time you see ImportError: No module named 'xyz', you'll know exactly where to look. You've got this.
Was this solution helpful?