ModuleNotFoundError on deploy but it works locally
THE SHORT ANSWER
ModuleNotFoundError means the deployed Python searched every folder on sys.path and found no module with that name. If the same code runs on your machine, the module exists in your local environment and not in the deployed one. The usual reasons: the package isn't in requirements.txt because you installed it globally or in another virtualenv; the import name differs from the package name (Pillow is imported as PIL); a file renamed only by changing its case on macOS or Windows kept its old name in Git; your own package isn't on the import path (a src layout, or the app starts from another folder); the package is a dev-only dependency the production install skips; or the server runs a Python version that removed the module, like distutils in 3.12.
What does ModuleNotFoundError mean?
Python's docs describe it as “a subclass of ImportError which is raised by import when a module could not be located.” The traceback ends with the name Python looked for:
Traceback (most recent call last):
File "/app/main.py", line 3, in <module>
import requests
ModuleNotFoundError: No module named 'requests'Read that name first. If it's a third-party package (requests, dotenv, PIL), the deployed environment didn't install it. If it's one of your own modules (app, utils, app.routes), the file is there but Python isn't looking in the right place, or the file isn't named what your import says.
Quick checklist
| Module in the error | Likely cause | Fix |
|---|---|---|
| A package you installed with pip | It isn't in requirements.txt | Reproduce in a fresh venv, add it, pin it |
PIL, cv2, bs4, dotenv, sklearn | The import name isn't the package name | Put the PyPI name in requirements.txt |
| Your own module, after a rename | Git kept the old capitalization | Rename it with git mv |
Your own package (app, myproject) | The project root or src/ isn't on sys.path | Start with python -m, --app-dir, --chdir or PYTHONPATH |
| A tool from your dev dependencies | The production install skips dev groups | Move it to the main dependencies |
distutils, pkg_resources, imghdr, cgi | A newer Python removed it | Pin the Python version, or add the replacement |
Is the package in requirements.txt?
This is the most common cause. At some point you ran pip install requests on your machine, globally or in a virtualenv you no longer use, and never added it to requirements.txt. Your machine has it, so the app runs. The server installs only what the file lists.
The quickest way to see what the server sees is to install from the file into an empty environment and run the app there:
python -m venv /tmp/clean . /tmp/clean/bin/activate python -m pip install -r requirements.txt python main.py # same ModuleNotFoundError as the deploy
Add each missing package and repeat until it starts. pip's docs describe pinning everything with python -m pip freeze > requirements.txt so installs are repeatable. Only run that inside the project's own venv: outside one it writes every package on your machine into the file.
Pin versions while you're there. A line with no version installs the newest release on every deploy, so a requirements file that built last month can pull a new major version today, and a major version is allowed to move or drop modules your code imports.
Also check that pip and your app use the same Python. The pip command on your PATH can belong to a different interpreter than python, which installs the package somewhere your app never looks. That is why Python's own docs show python -m pip install SomePackage. Compare python -m pip --version with pip --version: each prints the Python it belongs to.
Does the import name match the package name?
requirements.txt takes the name a package has on PyPI. Your code imports the module it installs, and the two aren't always the same:
| In requirements.txt | In your code |
|---|---|
Pillow | from PIL import Image |
opencv-python | import cv2 |
beautifulsoup4 | from bs4 import BeautifulSoup |
python-dotenv | from dotenv import load_dotenv |
scikit-learn | import sklearn |
Writing the import name in requirements.txt either fails the install or installs some other project that happens to use that name. When the error names a module you don't recognise as a package, look up which PyPI project provides it and add that.
Did you rename a file by changing only its case?
macOS and Windows file systems ignore case by default. Linux, which nearly every server runs, doesn't. Git tries to cope on the case-insensitive side. Its docs for core.ignoreCase: “if a directory listing finds "makefile" when Git expects "Makefile", Git will assume it is really the same file, and continue to remember it as "Makefile".”
So if you rename Utils.py to utils.py in your editor, your disk has utils.py and import utils works, but Git still tracks Utils.py. The server clones Utils.py, and import utils fails. The same happens with package folders. Ask Git what it has, then rename through Git in two steps:
git ls-files | grep -i utils # Utils.py <- the name the server gets git mv Utils.py utils_tmp.py git mv utils_tmp.py utils.py git commit -m "Rename Utils.py to utils.py"
Is your own code on the import path?
When the missing module is your own package, check where Python starts looking. From the docs on sys.path: “The first entry in the module search path is the directory that contains the input script, if there is one. Otherwise, the first entry is the current directory, which is the case when executing the interactive shell, a -c command, or -m module.”
That means python app/main.py puts app/ on the path, not the project root, so from app.db import engine inside it fails. Locally it often works anyway, because your IDE adds the project root to the path, or a PYTHONPATH in your shell profile does. Run it as a module from the project root instead: python -m app.main.
Projects with a src/ layout hit this on purpose. The Python Packaging guide says the layout “helps prevent accidental usage of the in-development copy of the code”: your package isn't importable from the project root until it's installed or src is on the path. Any one of these fixes it:
# src/myapp/main.py holds the app # uvicorn: --app-dir adds the folder to the Python path uvicorn myapp.main:app --app-dir src --host 0.0.0.0 --port $PORT # gunicorn: --chdir changes directory before loading the app gunicorn --chdir src myapp.main:app -b 0.0.0.0:$PORT # or set an environment variable PYTHONPATH=src # or install the project itself (pyproject.toml) pip install .
uvicorn's --app-dir defaults to the current directory, and gunicorn also has --pythonpath, a comma-separated list of directories to add.
Is it a dev-only dependency?
Production installs usually leave development dependencies out. Nixpacks, the builder several platforms use, runs poetry install --no-dev for Poetry projects and pdm install --prod for PDM. A requirements-dev.txt is never read unless your install command names it.
If application code imports something that only lives in a dev group, it works locally (you installed everything) and fails in production. Common examples are a seed script run at startup that imports a fake-data library, or a settings file that imports a debug toolbar. Move the package into the main dependencies, or guard the import so production never reaches it.
Did the server run a different Python version?
Some modules have left the standard library, so the same import works on one Python and fails on the next:
- Python 3.12 removed
distutils(PEP 632). What's New in 3.12 says “The third-party Setuptools package continues to provide distutils, if you still require it in Python 3.12 and beyond.” 3.12 also stopped pre-installing setuptools in new virtualenvs, soNo module named 'setuptools'or'pkg_resources'shows up on code that never listed it. Addsetuptoolsto your requirements. - Python 3.13 removed 19 legacy modules (PEP 594), including
cgi,cgitb,crypt,imghdr,pipesandtelnetlib. What's New in 3.13 lists a replacement for each, some as PyPI redistributions such asstandard-cgi.
The opposite direction breaks too: a package that only ships for newer Pythons won't install on an older one. Pin the version you develop on so both sides match:
python --version # Python 3.12.7 echo "3.12" > .python-version
What happens on EthioDeploy?
EthioDeploy builds Python apps with Nixpacks. It detects main.py, requirements.txt, pyproject.toml or Pipfile, creates a virtualenv at /opt/venv and installs into it: pip install -r requirements.txt, Poetry or PDM when their lock file is present (without dev groups, as above), or pipenv for a Pipfile. If you set your own Install command to a plain pip install -r requirements.txt, it still runs inside that venv.
Nixpacks' docs list Python 3.11 as its default. Add a .python-version file or set the NIXPACKS_PYTHON_VERSION environment variable to match the version you develop on. Environment variables you add in the project's settings, including PYTHONPATH=src, reach both the build and the running app. If the app sits in a subfolder of the repo, point the project's Root Directory at that folder so the right requirements.txt is found.
The full setup, including start commands for FastAPI and Flask, is in the Python deploy guide.
Questions
Your local environment has a module the deployed one doesn't. Usually the package was installed on your machine but never added to requirements.txt. Other causes are an import name that differs from the package name, a case-only file rename Git didn't record, a src layout that isn't on the import path, a dev-only dependency, or a different Python version.
Create a fresh virtualenv, install only from requirements.txt with python -m pip install -r requirements.txt, and run the app. It fails the same way the deploy did, one missing package at a time.
Python 3.12 removed distutils from the standard library. Add setuptools to your requirements, since it still provides distutils, or pin the app to the Python version it was written for.
pip and python are probably different interpreters. Install with python -m pip so the package goes into the Python that runs your app, and compare python -m pip --version with pip --version.
Make sure the folder that contains the package is on sys.path. Run python -m app.main from the project root instead of python app/main.py, or for a src layout use uvicorn --app-dir src, gunicorn --chdir src, PYTHONPATH=src or pip install . Also check git ls-files for a file whose capitalization differs from the import.
Sources
Checked on November 15, 2026:
- Python: Built-in exceptions
- Python: The initialization of the sys.path module search path
- Python: Installing Python modules
- What's New in Python 3.12
- What's New in Python 3.13
- pip: User guide
- Python Packaging: src layout vs flat layout
- Git: git-config (core.ignoreCase)
- Nixpacks: Python provider
- Uvicorn: Settings
- Gunicorn: Settings
- Pillow: Tutorial
- opencv-python on PyPI
- beautifulsoup4 on PyPI
- python-dotenv on PyPI
- scikit-learn: Installing
Push a repo. Get a URL.
Start freeJoin the EthioDeploy Telegram
Release notes as they ship, plus deploy help from the team and other developers.