1、创建文件夹
自己创建一个文件夹
2、打开文件夹
在PyCharm中打开创建得文件夹 File > Open
3、安装FastAPI和uvicore
打开Terminal,使用命令安装
pip install fastapi -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install uvicore -i https://pypi.tuna.tsinghua.edu.cn/simple
PS:镜像网站:
中国科学技术大学 : https://pypi.mirrors.ustc.edu.cn/simple
豆瓣:http://pypi.douban.com/simple/
阿里云:http://mirrors.aliyun.com/pypi/simple/
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
PSS: 若提示 ‘pip’ 不是内部或外部命令,也不是可运行的程序或批处理文件。 则需要进行系统配置。【找到Python的安装目录,一般目录地址为:"C:\Users\XXXX\AppData\Local\Programs\Python\Python38\Scripts",复制地址,在系统变量中的Path里新增,添加该目录即可,添加完后记的重启电脑后再安装】
4、创建项目
在文件中创建一个新的Python File
添加以下代码:
from fastapi import FastAPI
app = FastAPI()
import uvicorn
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
uvicorn.run("demo1:app", host="127.0.0.1", port=8080)
PS:run中的app为py文件的名字。
运行结果:
打开链接
运行成功!