原课程地址:
FastAPI Web框架 https://www.datawhale.cn/learn/summary/164本次难点:
https://www.datawhale.cn/learn/summary/164本次难点:
切换python的版本为3.10
作业过程
启动:
jupyter notebook
  
首先我们要确保自己的python版本是3.10
import sys
print(sys.version) 
 
第一个fastapi程序
!pip install fastapi 
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
    return {"message": "Hello World"}
if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8000)由于平时python版本用的是3.12,不符合课程要求,要切换python版本。
✅ 方法一:查看是否安装了 Python 3.10
-  运行以下命令看看是否安装了 Python 3.10,并找到它的路径: 
bashwhere python  
你之前看到的输出是:
makefileD:\Users\app\miniconda3\python.exe C:\Users\app\AppData\Local\Programs\Python\Python310\python.exe  
这里的:
bashC:\Users\app\AppData\Local\Programs\Python\Python310\python.exe  
就是你安装的 Python 3.10! 🎉
✅ 方法二:用完整路径运行 Python 3.10
你可以这样创建虚拟环境:
bash"C:\Users\app\AppData\Local\Programs\Python\Python310\python.exe" -m venv llm-universe-310  
这将创建一个名为 llm-universe-310 的虚拟环境,使用的是你系统中的 Python 3.10。
✅ 方法三:设置环境变量(可选)
如果你希望以后能直接用 python3.10 命令调用它,可以将这个路径加到你的系统环境变量的 Path 中:
makefileC:\Users\app\AppData\Local\Programs\Python\Python310\  
加完之后重新打开一个命令行窗口,运行:
bashpython3.10 --version  
就应该能识别了。
下一步:激活虚拟环境
创建成功后,在命令行中激活环境(在当前目录下):
bash.\llm-universe-310\Scripts\activate  
激活后你会看到前面变成 (llm-universe-310),然后就可以继续安装你需要的包(如 Jupyter、FastAPI、Uvicorn 等)了:
bashpip install jupyter fastapi uvicorn ipykernel  
然后:
bashpython -m ipykernel install --user --name llm-universe-310 --display-name "Python 3.10 (llm-universe)"  
之后你就能在 Jupyter Notebook 中选择 "Python 3.10 (llm-universe)" 内核。
✅ 下一步:启动 Jupyter Notebook 并选择内核
-  确保你现在还在虚拟环境 (llm-universe-310)中。
-  然后运行: 
bashjupyter notebook  
-  在浏览器中打开的 Jupyter 页面,点击 右上角 New → 你会看到一个新的内核选项: 
scssPython 3.10 (llm-universe)  
 
-  选择它,打开一个新的 Notebook。 
-  在代码单元中输入并运行以下代码确认版本: 
pythonimport sys print(sys.executable) print(sys.version)  
如果输出路径中包含:
..llm-universe-310\Scripts\python.exe 并且版本是 3.10.x,说明你现在 Jupyter Notebook 使用的是 你刚刚设置的 Python 3.10 虚拟环境 
在Jupyter notebook的格子中贴入以下代码:
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
    return {"message": "Hello World"}
if __name__ == '__main__':
    config = uvicorn.Config(app, host='0.0.0.0', port=8000)
    server = uvicorn.Server(config)
    await server.serve()
打开浏览器访问 http://127.0.0.1:8000

下面看不懂,太小白了,缺乏基础知识,不知道从哪里补起,对老师说一下抱歉!以后再学。



















