1、安装vscode
https://code.visualstudio.com/Download
下载最新版本后,win下直接安装。
2、安装python
https://www.python.org/downloads/
下载最新版本后,win下直接安装。
*vscode中运行测试python是否安装正确,vscode需要重启,否则重定向去win store中
3、安装python插件
4、安装flask
pip install flask
* 安装python后需要重启vscode,否则找不到pip
**安装模块报错,可运行 pip install –upgrade setuptools –timeout 480
5、安装peewee和flask_cors (playhouse为peewee部件)
pip install peewee flask_cors
peewee为ORM模块
6、建立debug文件
To generate a launch.json
file with Python configurations, do the following steps:
- Select the create a launch.json file link (outlined in the image above) or use the Run > Open configurations menu command.
- A configuration menu will open from the Command Palette allowing you to choose the type of debug configuration you want for the opened file. For now, in the Select a debug configuration menu that appears, select Python File.Note: Starting a debugging session through the Debug Panel, F5 or Run > Start Debugging when no configuration exists will also bring up the debug configuration menu, but will not create a launch.json file.
- The Python extension then creates and opens a
launch.json
file that contains a pre-defined configuration based on what you previously selected, in this case, Python File. You can modify configurations (to add arguments, for example), and also add custom configurations.
7、运行(RUN)即可(默认app.py)
建立了一个服务器
flask程序基础
from flask import Flask
app = Flask(__name__)
#Flask类只有一个必须指定的参数,即程序主模块或者包的名字,__name__是系统变量,该变量指的是本py文件的文件名,通过传入这个名字确定程序的根目录,以便获得静态文件和模板文件的目录。
@app.route(“/”)
def hello():
return “Hello World!”
if __name__ == ‘__main__’:
app.debug = False
app.run(host=’localhost’, port=5000)
访问http://localhost:5000/,会有 Hello World! 显示。
- 服务器中要改为app.run(debug=True, host=’0.0.0.0′, port=5000)
外部可见的服务器
运行服务器后,会发现只有您自己的电脑可以使用服务,而网络中的其他电 脑却不行。缺省设置就是这样的,因为在调试模式下该应用的用户可以执行 您电脑中的任意 Python 代码。
如果您关闭了调试器或信任您网络中的用户,那么可以让服务器被公开访问。 只要在命令行上简单的加上 –host=0.0.0.0 即可:
$ flask run –host=0.0.0.0
这行代码告诉您的操作系统监听所有公开的 IP 。
来自 <https://dormousehole.readthedocs.io/en/latest/quickstart.html>
8、PM2 运行进程守护
1)win下安装pm2
首先安装nodejs
然后安装pm2
npm install pm2 -g
如果太慢,可以换源(2023有效,淘宝源)
npm config set registry https://registry.npmmirror.com/
2)win下安装好后。
管理员模式运行CMD(重要,否则出错,powershell不行)
切换到app所在文件夹(重要,否则出错)
Pm2 start app.py
此时如果pm2 list 中status显示进程为error,需要更新一下pm2(why?)
- Pm2 update
3)可编写bat批处理完成PM2的启动
要注意,如果需要pause回显,可采用call pm2.。。。的方式
- 如果要win启动时运行,需要在系统中建立启动服务。
9、vue程序运行
npm run dev
注意:app.vue中的axios中的地址决定了使用局域网还是广域网