Skip to content
Node.js and Python2 min read

ModuleNotFoundError: No module named

ModuleNotFoundError: No module named 'flask'

Short answer

ModuleNotFoundError: No module named 'x' means the Python interpreter that runs your code can't find that package. Usually it was installed for a different Python or outside the virtual environment, so activate the right environment and run `pip install -r requirements.txt`, or install with `python -m pip` using the same interpreter that runs the app.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Causes
  2. How to fix it
  3. Prevention
  4. On ArabyHost

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.
  • pip and python belong to different installations (for example pip for 3.9 and python3 3.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.py or openai.py, which leads to messages like No 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

  1. Find out which Python runs the code:

    python -c "import sys; print(sys.executable)"
  2. Install with that same interpreter, so pip can’t target another one:

    python -m pip install flask
  3. Use a virtual environment per project:

    python -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt

    On Windows, activate with venv\Scripts\activate.

  4. Keep requirements.txt complete. After installing what you need locally, run pip freeze > requirements.txt and commit it.

  5. Check the import name against the table above or the package’s documentation.

  6. For your own modules, run from the project root and use python -m package.module instead of python package/module.py.

  7. Rename any file that shadows a library, then delete __pycache__.

  8. 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.txt and install from it on every server.
  • Always call python -m pip rather than bare pip.

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.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.