Skip to content
TaeyoungKim.dev

Python venv and pip: Why ModuleNotFoundError Appears After Installation

PythonWritten 2 min readTaeyoungKim
LinkedInX

If pip install succeeds but running the program raises ModuleNotFoundError, installation and execution may be using different Python interpreters. Comparing interpreter paths is usually faster than questioning the package name first.

Create a virtual environment for each project

Installing a package into .venv and running the program with system Python can cause ModuleNotFoundError. Using the same .venv interpreter for both operations keeps the environment boundary consistent.

bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install requests
python -m pip show requests

A virtual environment separates a project's packages from system Python. Even after activation, using python -m pip rather than a bare pip makes it clear which Python is installing the package.

For example, the terminal may use the Python in .venv while an IDE runs the file with system Python. An import can then fail immediately after installation. Before rebuilding the virtual environment, compare where the following commands point.

Inspect the paths directly

bash
which python
python -c "import sys; print(sys.executable)"
python -m pip --version

Check whether all three refer to the same .venv. The IDE's interpreter may differ from the terminal's, so select the virtual environment in the project settings as well. Inspect these paths before removing global packages or changing system Python.

bash
python -c "import requests; print(requests.__version__)"

If this command succeeds, installation and execution are aligned at least in the current terminal. If only the IDE fails, print sys.executable there and compare it with the terminal. Checking the two execution environments separately is faster than trying to repair them both at once.

Instead of committing the virtual environment itself, record the required packages and version ranges in a dependency file. Hardcoding environment-specific paths or using sudo pip install to change system Python makes the setup harder to reproduce and recover. Document the team's Python version and installation command together.

In automation where activation is inconvenient, you can call the environment's interpreter directly: .venv/bin/python -m pip install requests and .venv/bin/python app.py. On Windows, use the .venv\Scripts\python.exe path. The important point is that installation and execution use the same interpreter.

Key takeaways

A successful installation and a successful import are connected only when they use the same interpreter. Create a venv for each project, install with python -m pip, and inspect both the execution Python and pip paths. Keep a reproducible dependency list so another environment can install the same requirements.

Author

TaeyoungKim

Connecting technical foundations with implementation, verification, and production decisions.

#Python#venv#pip#ModuleNotFoundError

Read next