简体   繁体   English

Python 我无法在 flask 中打开文件“app.py”

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

I was using flask to develop a Python project.我正在使用 flask 开发 Python 项目。 It was working fine to start the backend program via python3 app.py under the backend_code/ directory.通过backend_code/目录下的python3 app.py启动后端程序工作正常。 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.当我移回目录folder_name/并运行backend_code/python3 app.py时它运行良好,但我尽量不这样做,因为它会导致我的后端代码中的某些文件路径读取无效。 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/ ).我不明白我启动了解释器并在目录backend_code/下运行 app.py 但错误消息显示它正在寻找 app.py 下project_name/显然找不到 app.py (它在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).我能记得的唯一可能导致此问题的事情是我将 pip 从 9.* 更新到 12.*(我已经将其降级,但错误仍然存在)。

Other information might be useful:其他信息可能有用:

I am using vscode.我正在使用 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!!为什么?感谢您的帮助!

I have the same sort of error, using Dash app (which is based on Flask).我有同样的错误,使用 Dash 应用程序(基于 Flask)。

As far as I got is that it could be related to sys.path.append in my several .py files.据我所知,它可能与我的几个.py文件中的sys.path.append相关。 But if I do not have this path appended I cannot load some functions in other folder (which is a DB helper).但是,如果我没有附加此路径,我将无法在其他文件夹(这是一个 DB 助手)中加载一些函数。

Anybody have any idea how to solve this?有人知道如何解决这个问题吗?

I'm using Ubuntu, VS Code and anaconda.我正在使用 Ubuntu、VS 代码和 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):使用解决方案编辑:如果我运行 werkzeug 的run_simple而不是作为开发服务器的本机app.run_server (如上面的警告所述),我能够解决问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM