跳转至

故障排除

每一节都先给出你看到的消息,再说明原因和解决方法。

fastkit: command not found

$ fastkit --version
bash: fastkit: command not found

shell 找不到 fastkit 命令。原因可能是安装 fastkit 的虚拟环境没有激活,也可能是 uv tool install 存放命令的文件夹不在你的 PATH 上(uv tool update-shell 会把它加进去)。激活虚拟环境(source .venv/bin/activate;Windows:.venv\Scripts\activate)之后,fastkit --version 就会打印 fastapi-fastkit, version 2.0.0。python -m fastapi_fastkit 会用当前激活环境中的 Python 运行同一个命令。请参阅安装。

项目已存在

$ fastkit init --name hello --no-venv -y
╭──────────────── Error ────────────────╮
│ ❌ Project 'hello' already exists.    │
╰───────────────────────────────────────╯

除非你明确要求,init 永远不会写入已有的文件夹。通常只需换一个名称即可解决。如果要尝试另外两种方法,请先提交或备份你的工作:

  • fastkit deleteproject hello 在你确认之后删除整个 hello 文件夹。
  • --in-place 把项目写入当前文件夹。模板不包含的文件会被保留,模板包含的文件(例如 README.md)会被覆盖;在终端中,它会先列出这些文件并请你确认,而 -y 会不经询问直接覆盖它们。

模板不存在

$ fastkit init --template fastapi-sqlmode --project-name notes
╭───────────────────────────────── Error ──────────────────────────────────╮
│ ❌ Template 'fastapi-sqlmode' does not exist. Did you mean               │
│ 'fastapi-sqlmodel', 'fastapi-single-module'? Run 'fastkit                │
│ list-templates' to see the available templates.                          │
╰──────────────────────────────────────────────────────────────────────────╯

fastkit 2.0 中删除的模板也会以同样的方式报错;从 fastkit 1.x 升级列出了它们的替代方案。

runserver 找不到虚拟环境

用 --no-venv 创建的项目,或者刚克隆下来的项目,都没有 .venv。fastkit runserver 会打印按项目的包管理器创建虚拟环境的命令;运行这些命令之后,再运行一次 fastkit runserver:

$ fastkit runserver
╭───────────────────────────────── Error ──────────────────────────────────╮
│ ❌ This FastAPI-fastkit project has no virtual environment (.venv), for  │
│ example because it was created with --no-venv. Create it and install the │
│ dependencies from the project directory with:                            │
│   uv sync                                                                │
│ then run 'fastkit runserver' again.                                      │
╰──────────────────────────────────────────────────────────────────────────╯
Do you want to continue with the current Python instead? [y/N]: n

没有安装 uv

如果已安装 uv,fastkit 就使用 uv,否则使用 pip。这时,汇总表格中的“Package manager”一行会显示 pip (uv is not installed),“Next steps”面板和 README.md 中给出的也都是 pip 命令。项目用 pip 也能正常工作。如果想使用 uv,请安装 uv 后重新创建项目,或者传入 --package-manager。

Poetry 版本低于 2.0

$ fastkit init --name shop --level layered --database None --package-manager poetry -y
...
╭───────────────────────────────── Error ──────────────────────────────────╮
│ ❌ Project creation failed: Poetry 1.7.1 is installed, but projects      │
│ generated by FastAPI-fastkit need Poetry 2.0 or later (they keep their   │
│ dependencies in the standard [project] table, which older Poetry cannot  │
│ read). Upgrade Poetry with 'poetry self update' (or 'pipx upgrade        │
│ poetry' if pipx installed it), or choose another package manager with    │
│ --package-manager uv, pdm or pip. Nothing was created.                   │
╰──────────────────────────────────────────────────────────────────────────╯

端口已被占用

另一个进程已经在监听这个端口,这个进程通常是之前启动的服务器。runserver 会以服务器的退出状态码退出:

$ fastkit runserver
...
╭───────────────────── Success ──────────────────────╮
│ ✨ Starting FastAPI server at 127.0.0.1:8000...    │
╰────────────────────────────────────────────────────╯
...
ERROR:    [Errno 48] Address already in use
╭───────────────────────────────── Error ──────────────────────────────────╮
│ ❌ The FastAPI server stopped with exit status 3; its messages above say │
│ why.                                                                     │
╰──────────────────────────────────────────────────────────────────────────╯

请停止另一个服务器,或者用 --port 选择一个空闲的端口。错误编号因系统而异。

项目名称与某个依赖同名

$ fastkit init --template fastapi-sqlmodel --project-name sqlmodel --no-venv
╭───────────────────────────────── Error ──────────────────────────────────╮
│ ❌ The project name 'sqlmodel' is also the name of one of its            │
│ dependencies (sqlmodel>=0.0.42): the package manager would take the      │
│ project for that package and not install it, so the app could not import │
│ it. Choose another project name.                                         │
╰──────────────────────────────────────────────────────────────────────────╯

Windows

  • 激活虚拟环境时,请使用 .venv\Scripts\activate,而不是 source .venv/bin/activate。
  • fastkit runserver 在所有系统上都能启动开发服务器。生成的项目中的 scripts/*.sh 辅助脚本需要在 Git Bash 或 WSL 中运行。