Skip to content

Switch to importlib breaks scripts with app.run() #5160

Description

@The-Compiler

With a trivial script using app.run() such as:

from flask import Flask

app = Flask(__name__)

if __name__ == "__main__":
    app.run(debug=True)

The current git main breaks with:

Traceback (most recent call last):
  File "/home/florian/tmp/flask/app.py", line 3, in <module>
    app = Flask(__name__)
          ^^^^^^^^^^^^^^^
  File "/home/florian/tmp/flask/src/flask/app.py", line 376, in __init__
    instance_path = self.auto_find_instance_path()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/florian/tmp/flask/src/flask/app.py", line 630, in auto_find_instance_path
    prefix, package_path = find_package(self.import_name)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/florian/tmp/flask/src/flask/scaffold.py", line 898, in find_package
    package_path = _find_package_path(import_name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/florian/tmp/flask/src/flask/scaffold.py", line 858, in _find_package_path
    spec = importlib.util.find_spec(root_mod_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib.util>", line 114, in find_spec
ValueError: __main__.__spec__ is None

This seems to be a regression due to 84e11a1 from #5157.

Environment:

  • Python version: 3.11.4
  • Flask version: git main

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions