Python, Flask and the dreaded error: No module named ‘imp’

When trying to run a flask application under vscode, dev container, python 3.12.7 64 bit, I started getting these errors:

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
Traceback (most recent call last):
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 3433, in <module>
    main()
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 3426, in main
    globals = debugger.run(setup['file'], None, None, is_module)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 2437, in run
    m = save_main_module(file, 'pydevd')
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_utils.py", line 32, in save_main_module
    from imp import new_module
ModuleNotFoundError: No module named 'imp'
vscode ➜ /workspaces/pyscheduler (adjusting-scheduler) $ 

After lots of non helpful answers (at least not for me!) I stumbledacreoss this:

https://github.com/microsoft/debugpy/issues/808

Turns out this really helpful comment solved the issue for me!

https://github.com/microsoft/debugpy/issues/808#issuecomment-1006990063

Flask has its own built-in debugger, which IIRC is enabled by default in the dev environment. If you try to use two debuggers at once, they’re going to fight over sys.settrace hook with basically unpredictable results – I suspect this is what’s happening here.

This can be controlled by setting FLASK_DEBUG to 0 – can you try that and see if it helps?

After setting this line:

    app.run(debug=True)

to this:

    app.run(debug=False)

My app started working again! Hopefully you find this helpful!


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *