Python I can't open file 'app.py' in flask

Viewed 1548

I was using flask to develop a Python project. It was working fine to start the backend program via python3 app.py under the backend_code/ directory. However, when I tried it today, it showed

my_user_namedeMBP:backend_code my_user_name$ python3 app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
/Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python: can't open file '/Users/my_username/folder_name/**project_name/app.py**': [Errno 2] No such file or directory

Here is the project structure:

+-- folder_name
    +-- project_name
      +-- frontend_code
      |
      +-- backend_code
          +-- app.py

It works well when I moved back to directory folder_name/ and run backend_code/python3 app.py but I try not to do that because it will cause some files path reading in my backend code invalid. I dn't understand I started the interpreter and run app.py under directory backend_code/ but the error message shows that it is looking for app.py under project_name/ which obviously cannot find app.py (It is under project_name/backend_code/).

The only thing I can recall which might cause this issue is I updated my pip from 9.* to 12.* (I downgraded it already but error still persists).

Other information might be useful:

I am using vscode.

  pip -V => pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)
  pip3 -V => pip 9.0.3 from 
   /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (python 3.6) 
  python --version => Python 2.7.15
  python3 --version => Python 3.6.5 

Could anyone tell me what I should do? Why is that? Appreciate your help!!

1 Answers

I have the same sort of error, using Dash app (which is based on Flask).

As far as I got is that it could be related to sys.path.append in my several .py files. But if I do not have this path appended I cannot load some functions in other folder (which is a DB helper).

Anybody have any idea how to solve this?

I'm using Ubuntu, VS Code and anaconda.

Thank you for you help.

EDIT with solution: I was able to fix the issue if I run werkzeug's run_simple instead of the native app.run_server which is a development server (as noted above with the warning):

    from werkzeug.serving import run_simple
    run_simple("0.0.0.0", 8050, app.server, use_debugger=True)
Related