0

I have a very simple flask application with 2 files app.py and test.py both under same main folder.

app.py:

from flask import Flask
import subprocess

app = Flask(__name__)
@app.route("/")
def home():
    proc = subprocess.Popen(['python', 'test.py'])
    return "Hello, Flask!"

test.py:

def main():
  print('PING')  

if __name__ == '__main__':
  main()

I created the virtual environment and added flask into it. When I run app.py using VSCode Python:Flask debugging I got the 'No module named test.py' error like below:

  • Serving Flask app 'app.py' (lazy loading)
  • Environment: development
  • Debug mode: on
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  • Restarting with stat
  • Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. 127.0.0.1 - - [08/Dec/2021 12:03:46] "GET / HTTP/1.1" 200 - No module named test.py

But when I run it using integrated terminal by 'python -m flask run' everything works fine and 'PING' being printed like below:

  • Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  • Debug mode: off
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [08/Dec/2021 12:00:02] "GET / HTTP/1.1" 200 - PING

I guess I miss some python debugging configuration but cannot realize which one. What should I add in order to fix it during debugging?

iweins
  • 1
  • How about `from test import main; main()`? – Klaus D. Dec 08 '21 at 10:39
  • Now, the problem is in much bigger application where we need subprocess.Popen to start an async process. Here just a simplified fragment to show that such approach doesn't work in debugging – iweins Dec 08 '21 at 10:45
  • Your problem might be related to not [setting the working directory](https://stackoverflow.com/q/38623138/3929826) in VSC. But in gerenal it is better to import the function and run it (directly, in a thread or in a new process). – Klaus D. Dec 08 '21 at 10:49
  • It didn't help. I added cwd into launch.json like this: > "version": "0.2.0", "configurations": [ { "name": "Python: Flask", "type": "python", "request": "launch", "module": "flask", "cwd": "${workspaceFolder}", "env": { "PYTHONPATH": "${cwd}", "FLASK_APP": "app.py", "FLASK_ENV": "development" }, "args": [ "run", "--no-debugger" ], "jinja": true } ] } same result. – iweins Dec 08 '21 at 11:37

0 Answers0