pallets/flask · error · RuntimeError
No root path can be found for the provided module {import_na
Error message
No root path can be found for the provided module {import_name!r}. This can happen because the module came from an import hook that does not provide file name information or because it's a namespace package. In this case the root path needs to be explicitly provided. What it means
Flask(import_name) resolves the application's root_path by importing the named module and reading its __file__ so it can locate templates, static files, and instance folders. Namespace packages (PEP 420) and modules created by import hooks have no __file__, so get_root_path cannot derive a filesystem location and raises RuntimeError telling you to supply it explicitly.
Source
Thrown at src/flask/helpers.py:631
# Loader does not exist or we're referring to an unloaded main
# module or a main module without path (interactive sessions), go
# with the current working directory.
if loader is None:
return os.getcwd()
if hasattr(loader, "get_filename"):
filepath = loader.get_filename(import_name) # pyright: ignore
else:
# Fall back to imports.
__import__(import_name)
mod = sys.modules[import_name]
filepath = getattr(mod, "__file__", None)
# If we don't have a file path it might be because it is a
# namespace package. In this case pick the root path from the
# first module that is contained in the package.
if filepath is None:
raise RuntimeError(
"No root path can be found for the provided module"
f" {import_name!r}. This can happen because the module"
" came from an import hook that does not provide file"
" name information or because it's a namespace package."
" In this case the root path needs to be explicitly"
" provided."
)
# filepath is import_name.py for a module, or __init__.py for a package.
return os.path.dirname(os.path.abspath(filepath)) # type: ignore[no-any-return]
@cache
def _split_blueprint_path(name: str) -> list[str]:
out: list[str] = [name]
if "." in name:
out.extend(_split_blueprint_path(name.rpartition(".")[0]))View on GitHub ↗ (pinned to 6a2f545bfd)
Solutions
- Pass root_path explicitly: Flask(__name__, root_path=os.path.dirname(os.path.abspath(__file__))).
- Add an __init__.py to the package so it becomes a regular package with __file__.
- Use the concrete module's __name__ (Flask(__name__)) from a real .py file instead of a namespace package name.
- In frozen apps, compute the bundle path (e.g. sys._MEIPASS for PyInstaller) and pass it as root_path.
Example fix
# before
app = Flask('company') # 'company' is a namespace package
# after
import os
app = Flask(__name__, root_path=os.path.dirname(os.path.abspath(__file__))) Defensive patterns
Strategy: validation
Validate before calling
import importlib.util, os spec = importlib.util.find_spec(import_name) has_file = spec is not None and spec.origin not in (None, 'namespace') root = os.path.dirname(os.path.abspath(spec.origin)) if has_file else os.getcwd() app = Flask(import_name, root_path=root)
Type guard
import importlib.util
def module_has_root_path(import_name: str) -> bool:
spec = importlib.util.find_spec(import_name)
return spec is not None and spec.origin not in (None, 'namespace') Prevention
- Use Flask(__name__) from a real module file, not from a REPL, frozen executable, or namespace package
- Pass root_path= explicitly when running under PyInstaller/zipapp/import hooks
- Ensure your package has an __init__.py so it isn't treated as a namespace package
When it happens
Trigger: Flask('mypkg') where mypkg is a namespace package (a directory with no __init__.py, or a package split across multiple dirs); import_name pointing at a module loaded from a zip/frozen bundle or custom finder whose loader lacks get_filename and the module lacks __file__.
Common situations: Forgetting __init__.py in a src-layout package; PyInstaller/zipapp/frozen deployments; monorepos using implicit namespace packages for shared roots; passing a top-level namespace like 'company' instead of the concrete subpackage.
Related errors
- Working outside of application context. Attempted to use fu
- Working outside of request context. Attempted to use functi
- The session is unavailable because no secret key was set. S
- The environment variable {variable_name!r} is not set and as
- 'after_this_request' can only be used when a request context
AI-assisted analysis of pallets/flask@6a2f545bfd (2026-07-31).
Data as JSON: /data/errors/8fddface7b8aa965.json.
Report an issue: GitHub ↗.