Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

部署到aws ec2  上 flask run [OPTIONS] Error: Could not locate Flask application. #17

Closed
tianke0711 opened this issue Oct 17, 2021 · 71 comments

Comments

@tianke0711
Copy link

No description provided.

@tianke0711
Copy link
Author

tianke0711 commented Oct 17, 2021

我尝试着简单部署到aws ec2 ubuntu上 我运行flask run出现了下面问题,我没有用venv环境。我需要修改哪些地方啊。

flask run
Usage: flask run [OPTIONS]

Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.

For more information see http://flask.pocoo.org/docs/latest/quickstart/

我是通过pycharm上传的 我在ubuntu 根目录建了一个项目文件夹,然后上传blogin文件的这个项目文件夹下面。

@tianke0711 tianke0711 changed the title 部署到aws 而出 部署到aws ec2  上 flask run [OPTIONS] Error: Could not locate Flask application. Oct 17, 2021
@weijiang1994
Copy link
Owner

在项目根目录新建.flaskenv文件,在其中输入以下内容

FLASK_APP=blogin
FLASK_ENV=production

@tianke0711
Copy link
Author

我把以前的development哪个删除了,写了以下的。
$ nano .flaskenv

GNU nano 2.9.3 .flaskenv

FLASK_APP=blogin
FLASK_ENV=production

还是老问题

flask run
Usage: flask run [OPTIONS]

Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.

For more information see http://flask.pocoo.org/docs/latest/quickstart/

@tianke0711
Copy link
Author

tianke0711 commented Oct 17, 2021

我在terminal设置了 export FLASK_APP=blogin

flask run
Usage: flask run [OPTIONS]

Error: The file/path provided (blogin) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py

@weijiang1994
Copy link
Owner

用虚拟环境试下

cd Blogin
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
flask run

@weijiang1994
Copy link
Owner

还有看看有没有安装python-dotenv这个库

@tianke0711
Copy link
Author

@weijiang1994 安装好python-dotenv 有这个错误。

flask run

  • Serving Flask app 'blogin' (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: off
    Usage: flask run [OPTIONS]
    Try 'flask run --help' for help.

Error: While importing 'blogin', an ImportError was raised.

@weijiang1994
Copy link
Owner

看看运行flask run的执行路径是否有误。

@tianke0711
Copy link
Author

tianke0711 commented Oct 18, 2021

@weijiang1994
我运行flask run就是上面的哪个错误
Error: While importing 'blogin', an ImportError was raised.

执行路径是否有误。--->执行路径啥意思

@weijiang1994
Copy link
Owner

就是你是不是在Blogin文件夹下面执行的flask run

@tianke0711
Copy link
Author

@weijiang1994 我是在Blogin文件夹下面 不过这个文件夹是我ubuntu建的,因为pycharm上传的时候 好像只是把local里的Blogin里的所有文件全部上传,并没有上传文件夹Blogin。所以我在ec2 ubuntu重新建了一个Blogin文件夹。然后local直接把Blogin里python文件上传上去。

@weijiang1994
Copy link
Owner

建议用git操作,你不是fork了我的仓库吗,你把你的修改提交到你fork的远程仓库中去,然后在云服务器上直接git clone你的代码

@tianke0711
Copy link
Author

好的 我也试试。我想知道啥原因导致运行不行

@tianke0711
Copy link
Author

试了git clone的方法 ubuntu服务器 同样的错误

@weijiang1994
Copy link
Owner

信任我的话,可以把你aws服务器的 IP 登录名 登录密码 发我邮箱 804022023@qq.com,我帮你部署

@tianke0711
Copy link
Author

tianke0711 commented Oct 18, 2021

@weijiang1994 可以啦 gitclone后,建立。flaskenv文件。我把production改成development。 很感谢你的热情。
刚开始的时候production

  • Environment: production
    WARNING: This is a development server. Do not use it in a production deployment.

@tianke0711
Copy link
Author

flask run

  • Serving Flask app "blogin" (lazy loading)
  • Environment: development
  • Debug mode: on
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 985-679-113

@weijiang1994
Copy link
Owner

ok

@tianke0711
Copy link
Author

@weijiang1994 flask run运行后,我想打开aws url地址看看像local那样。 但是打开就是错误。
浏览器不能连接到服务器。我昨天测试简单的flask app是可以的

@weijiang1994
Copy link
Owner

flask run --host=0.0.0.0

@tianke0711
Copy link
Author

我运行啦 还是这样的问题
flask run --host=0.0.0.0

  • Serving Flask app "blogin" (lazy loading)
  • Environment: development
  • Debug mode: on
  • Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 985-679-113

@weijiang1994
Copy link
Owner

亚马逊服务器IP多少?

@tianke0711
Copy link
Author

52.197.136.131

@weijiang1994
Copy link
Owner

图片
你这地址ping不通

@tianke0711
Copy link
Author

是这个 我也ping不成功

@tianke0711
Copy link
Author

是不是要把上次测试flask的log删除掉啊, 那个简单的测试app

@weijiang1994
Copy link
Owner

ping不通说明你服务器有问题,跟这个没啥关系。你买的亚马逊哪个地区的?ping不通可能是国内防火墙把这个IP屏蔽掉了。

@tianke0711
Copy link
Author

我买的tokyo地区

@weijiang1994
Copy link
Owner

那国内大概率裸连不上,国内的腾讯云也不错,为什么要买亚马逊的呢...

@tianke0711
Copy link
Author

@weijiang1994 现在可以像local那样运行啦。但是页面说无法连接到服务器

@tianke0711
Copy link
Author

Job "network_monitor (trigger: interval[0:01:00], next run at: 2021-10-24 09:12:02 UTC)" raised an exception
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.6/site-packages/apscheduler/executors/base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/home/ubuntu/SciSci/blogin/task.py", line 160, in network_monitor
with open('/var/log/nginx/access.log', 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/var/log/nginx/access.log'
Job "network_monitor (trigger: interval[0:01:00], next run at: 2021-10-24 09:12:02 UTC)" raised an exception
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.6/site-packages/apscheduler/executors/base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/home/ubuntu/SciSci/blogin/task.py", line 160, in network_monitor
with open('/var/log/nginx/access.log', 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/var/log/nginx/access.log'

@tianke0711
Copy link
Author

我想问一下 在服务器上没有用nginx部署 ,你的程序仅仅用flask run后 通过ip地址是可以访问的吧?

@tianke0711
Copy link
Author

240-215.ap-northeast-1.compute.amazonaws.com 拒绝了我们的连接请求。
请试试以下办法:

@weijiang1994
Copy link
Owner

可以通过IP地址访问,但是你得确定你得IP地址能被国内访问到。

@tianke0711
Copy link
Author

现在打开后出现了
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1698, "Access denied for user 'root'@'localhost'")
(Background on this error at: http://sqlalche.me/e/13/e3q8)

@weijiang1994
Copy link
Owner

数据库连接用户名、密码错了...

@tianke0711
Copy link
Author

@weijiang1994 show tables 咋是空的啊 我运行了flask db init啦

@tianke0711
Copy link
Author

use blog;
Database changed
mysql>
mysql> show tables;
Empty set (0.00 sec)

@tianke0711
Copy link
Author

flask db init
0: here is begin
I am here
[2021-10-24 12:17:31,554] DEBUG in init: Scheduler Started,---------------
Error: Directory migrations already exists and is not empty

@weijiang1994
Copy link
Owner

image

@tianke0711
Copy link
Author

@weijiang1994 flask initdb
File "/home/ubuntu/.local/lib/python3.6/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1698, "Access denied for user 'root'@'localhost'")
(Background on this error at: http://sqlalche.me/e/13/e3q8)

@weijiang1994
Copy link
Owner

这不是跟上面的错误一样?数据库连接配置改了啊

@tianke0711
Copy link
Author

@weijiang1994 OK啦
sudo mysql -u root # I had to use "sudo" since it was new installation

mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;

sudo service mysql restart

@weijiang1994
Copy link
Owner

你也可以新建用户,不一定要使用mysql的root用户,可以看这篇文章https://2dogz.cn/blog/article/30/

@tianke0711
Copy link
Author

@weijiang1994 OK 谢谢 我好好看看。 你有时间可以把你做博客网站的记录一步步写下来。

@tianke0711
Copy link
Author

tianke0711 commented Oct 25, 2021

你好 为啥老是出现下面错误 我感觉需要修改数据库连接的参数,我想问一下 哪里设置

sqlalchemy.exc.TimeoutError
sqlalchemy.exc.TimeoutError: QueuePool limit of size 10 overflow 10 reached, connection timed out, timeout 30 (Background on this error at: http://sqlalche.me/e/13/3o7r)

@weijiang1994
Copy link
Owner

你好 为啥老是出现下面错误 我感觉需要修改数据库连接的参数,我想问一下 哪里设置

sqlalchemy.exc.TimeoutError sqlalchemy.exc.TimeoutError: QueuePool limit of size 10 overflow 10 reached, connection timed out, timeout 30 (Background on this error at: http://sqlalche.me/e/13/3o7r)

这个不是很清楚,可以看看这个https://stackoverflow.com/questions/24956894/sql-alchemy-queuepool-limit-overflow

@tianke0711
Copy link
Author

@weijiang1994 添加博客的时候的 点击submit时候,出现了错误。

File "/home/ubuntu/.local/lib/python3.6/site-packages/whoosh/index.py", line 421, in init
TOC.read(self.storage, self.indexname, schema=self._schema)
File "/home/ubuntu/.local/lib/python3.6/site-packages/whoosh/index.py", line 664, in read
segments = stream.read_pickle()
File "/home/ubuntu/.local/lib/python3.6/site-packages/whoosh/filedb/structfile.py", line 245, in read_pickle
return load_pickle(self.file)
ValueError: unsupported pickle protocol: 5

@weijiang1994
Copy link
Owner

你自己google搜一下,我也不清楚什么原因

@tianke0711
Copy link
Author

@weijiang1994 可能是python版本原因,python3.8支持5,而现在服务器的python版本是3.6. 我也不知道咋解决除了升级以外。你开发的python版本是啥?

@weijiang1994
Copy link
Owner

我服务器端部署的也是3.5可以正常运行,开发环境3.6 3.8都可以正常运行

@weijiang1994
Copy link
Owner

图片

@tianke0711
Copy link
Author

@weijiang1994 我local3.8可以啊 !难道上传文件时候把3.8环境文件上传进去了吗。

@weijiang1994
Copy link
Owner

不清楚

@tianke0711
Copy link
Author

@weijiang1994 你可知道程序里哪里可能产生pickle(self.file)pickle 格式,估计保存h5格式。其他创建类别以及上传图片都可以。估计哪里有python3.8运行时候保存的环境文件。

@weijiang1994
Copy link
Owner

models.py中找到图中的代码段,将红圈中的装饰器注释掉
图片

@tianke0711
Copy link
Author

@weijiang1994 OK啦 只要删除掉
/whooshee/blog$ l里面的文件.seg 和.toc文件
MAIN_WRITELOCK MAIN_megxx9m909gsh1v1.seg _MAIN_16.toc

估计这两个文件在local生成的。

@weijiang1994
Copy link
Owner

图片
我在.gitignore文件中添加了这个路劲,不会上传到github上去,除非你是把本地的文件直接通过ftp的方式推上去。

@tianke0711
Copy link
Author

tianke0711 commented Oct 27, 2021

@weijiang1994 我scp上传上去的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants