Each Python installation, and each virtual environment, has its own folder of installed packages. When code runs import flask, Python searches only the folders of the interpreter that is running it. If the package was installed somewhere else, you get ModuleNotFoundError, even though pip list in another terminal shows it.
Causes
- The virtual environment isn’t active, so the system Python runs the code.
pipandpythonbelong to different installations (for examplepipfor 3.9 andpython33.11).- The package is missing from
requirements.txt, so the server never installed it. - The import name differs from the package name you installed.
- cron or the app server runs a different interpreter than your terminal.
- A project file shadows the library, such as
telegram.pyoropenai.py, which leads to messages likeNo module named 'telegram.ext'; 'telegram' is not a package. - Your own module isn’t on the path because the script runs from another folder.
| You install | You import |
|---|---|
python-telegram-bot |
telegram |
beautifulsoup4 |
bs4 |
Pillow |
PIL |
python-dotenv |
dotenv |
opencv-python |
cv2 |
PyYAML |
yaml |
scikit-learn |
sklearn |
How to fix it
-
Find out which Python runs the code:
python -c "import sys; print(sys.executable)" -
Install with that same interpreter, so pip can’t target another one:
python -m pip install flask -
Use a virtual environment per project:
python -m venv venv source venv/bin/activate pip install -r requirements.txtOn Windows, activate with
venv\Scripts\activate. -
Keep
requirements.txtcomplete. After installing what you need locally, runpip freeze > requirements.txtand commit it. -
Check the import name against the table above or the package’s documentation.
-
For your own modules, run from the project root and use
python -m package.moduleinstead ofpython package/module.py. -
Rename any file that shadows a library, then delete
__pycache__. -
In cron and service files, use the environment’s Python by full path:
/home/USER/venv/bin/python /home/USER/bot/bot.py
If pip refuses to install with externally-managed-environment, see that error: it’s the system telling you to use a virtual environment.
Prevention
- One virtual environment per project, never system-wide installs.
- Pin versions in
requirements.txtand install from it on every server. - Always call
python -m piprather than barepip.
On ArabyHost
On ArabyHost AI/Developer plans, cPanel’s Setup Python App creates a virtual environment for each app with Python 3.9 to 3.12. Add requirements.txt under Configuration files and click Run Pip Install, or over SSH copy the command shown at the top of the app’s page, which looks like source /home/USER/virtualenv/APP/3.11/bin/activate, then run pip install -r requirements.txt. Bots and workers kept alive with cron must call that environment’s bin/python by full path. Step-by-step setups: Flask, Django and Telegram bots.
FAQ
I installed the package with pip but still get the error. Why?
`pip` installed it for a different Python than the one running your code. Run `python -m pip install package` with the exact interpreter you use to start the app, or activate the virtual environment first.
Why does my script work in the terminal but fail in cron?
Cron doesn't activate your virtual environment and uses a minimal PATH, so `python` there is the system interpreter. Use the full path to the environment's Python in the cron line.
What does 'x' is not a package mean?
A file in your project has the same name as the library, such as `telegram.py`, and Python imports it instead. Rename the file and delete the `__pycache__` folder.