FastAPIenvironment-setup and project initialization
📂 Stage: Stage 1 - Rapid Foundation Building (Basics) 🔗 Related chapters: fastapi-intro-advantages · hello-world-app
Table of contents
Quickly start pre-check
FastAPI has very low machine requirements, and almost any computer that can run Python can easily get started.
Basic software and hardware requirements
- Operating system: Windows 7 and above / macOS 10.12 and above / mainstream Linux distributions
- Python version: 3.7 or above (the latest stable version of 3.10, 3.11 or 3.12 is highly recommended)
- Hardware Configuration: 2GB RAM, 500MB free disk space, any modern CPU
First check if you have Python in your computer
Open a terminal (PowerShell or CMD for Windows, Terminal for Mac/Linux) and type the following commands to confirm the environment:
If it prompts "command not found" or the version is too low, please install or upgrade Python according to your system:
- Windows: Visit Python 官网 to download the installation package. Be sure to check "Add Python to PATH" during installation
- Mac: It is recommended to use Homebrew to install:
brew install python3 - Linux: Use the built-in package manager, such as Ubuntu:
sudo apt install python3 python3-pip
After the installation is complete, upgrade pip to the latest version to avoid strange compatibility issues in the future:
Isolation environment: Python virtual environment
**Whether it is a practice project or a formal product, it is strongly recommended to use a virtual environment for development! ** The virtual environment can isolate the Python packages required by different projects, completely solving conflicts such as "Project A must use the old version, and Project B must use the new version."
Just use the official venv that comes with it
After Python 3.3, the standard library comes with a lightweight virtual environment toolvenv, no need to install anything additional.
Create and activate virtual environment
After successful activation, the command line will appear in front of(venv)The logo indicates that you have entered the virtual environment, and all subsequent pip installations will only be installed in this environment.
Common management commands
Core components: FastAPI and Uvicorn
FastAPI itself is a Web framework, responsible for defining interfaces, handling data verification, etc.; however, it does not "run" directly. It requires an ASGI server to receive network requests. The official partner is Uvicorn.
Installation method: choose as needed
💡 Suggestion: Use it when you first start learning
fastapi[all]Very worry-free; use the accurate version when entering production projects +requirements.txtto control dependencies.
Use requirements.txt to lock the environment
When you share your project with others or deploy it on different computers, you canrequirements.txtQuickly restore an identical environment.
After the other party gets the project, it only needs one line of command:
Standard project structure construction
A clear project structure allows you not to panic when the code increases, while laying the foundation for team collaboration and function expansion.
Recommended project layout (easily scalable to large projects)
Write these key documents first
1. .gitignore(Prevent junk files from being uploaded to Git)
2. app/main.py(Application entrance)
VS Code development tool configuration
VS Code is the most popular editor for Python/FastAPI development. After installing the following plug-ins and doing a little configuration, development efficiency will be greatly improved.
1. Required plug-ins
Open the "Extension" icon on the left side of VS Code, search and install:
- Python (official, providing basic functions such as syntax highlighting, debugging, code completion, etc.)
- Pylance (powerful type checking and auto-completion)
- Ruff (an extremely fast Python code inspection + formatting tool, which can replace Flake8, isort, etc.)
2. Add local configuration to the project
Create in the project root directory.vscode/settings.json, the contents are as follows:
In this way, everyone in the team can write code using unified rules, reducing "format wars".
One-click verification development environment
After all the parts are ready, start your first FastAPI application with the following command:
Parameter explanation:
app.main:app→ module pathapp/main.pyFastAPI instance inapp--reload→ Automatically restart as soon as the code is modified, suitable for development and debugging--host 0.0.0.0→ Allow other devices in the same LAN to access (if you only want local access, you can remove it or use127.0.0.1)--port 8000→ Listen to port 8000 (if not written, the default is 8000)
Open the browser and see the effect
After the service is started, enter the following address in the browser:
- Root path:
http://127.0.0.1:8000/→ Return{"message": "欢迎来到FastAPI演示项目!"} - Health Check:
http://127.0.0.1:8000/health→ Return to service status - Swagger UI (interactive interface document):
http://127.0.0.1:8000/api/docs→ Click “Try it out” to test the interface directly - ReDoc (the document style is more concise):
http://127.0.0.1:8000/api/redoc
If everything can be accessed normally, congratulations, FastAPI development environment-setup is successful!
High Frequency Trampling Guide
1. "Unable to load file..." when activating virtual environment on Windows
error message:无法加载文件 ...,因为在此系统上禁止运行脚本
Solution: Open PowerShell as administrator, execute the following line of commands, and then reactivate:
2. Prompt "Address already in use" when starting
error message:OSError: [Errno 48] Address already in use
Cause: Port 8000 is already occupied by other programs.
Solution: Change the port to start, for example--port 8001, or find the program occupying the port in the terminal and close it (newbies are advised to change the port directly).
3. Not foundappmodule
error message:ModuleNotFoundError: No module named 'app'
Common reason: You are not in the project root directory (my-fastapi-demo) to run the command, but mistakenly enteredappfolder.
Correct Practice: Make sure themy-fastapi-demoWhen this layer is started, there should be something in that directoryappfolder andvenvfolder.
4. The package is obviously installed, but it says it cannot be found when running it.
Mostly it's because the virtual environment is not activated. Every time you open a new terminal, remember to activate the virtual environment first (see the command before(venv)flag), and then run the project.
Summarize
Now that you have a clean, professional FastAPI development environment, the next step is to:
- Follow hello-world-app to write the first interface that can receive parameters and return data;
- Play with the built-in Swagger UI 交互式文档 more and experience the pleasure of one-click debugging;
- Follow this project structure and start building your own functional modules.

