Repository navigation
Mr. Hong #2121
Replies: 3 comments 1 reply
|
Run it as a module from the folder above the package instead of running the file. Your three files sit directly in cd /Users/hongjunjie01
my-fastapi-project/venv/bin/python -m my-fastapi-project.appI ran that layout here on Python 3.13 and the hyphenated folder name works with Q2: The "Process finished with exit code 1" line means PyCharm's Run button ran the file as a script. In Run > Edit Configurations, switch the target from "Script path" to "Module name", enter |
|
Sorry , I have another question in chapter 'Simple Hero API with FastAPI' (https://sqlmodel.tiangolo.com/tutorial/fastapi/simple-hero-api/) I also copy webpage code to Pycharm. Pycharm shows problem: on_event is deprecated, use lifespan event handlers instead.Read more about it in the [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/). Pycharm runs shows problem: @app.on_event("startup") Process finished with exit code 0 |
|
I find subsequent chapters also the same question. Please help me fix this question: Read more about it in the [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/). |


Uh oh!
There was an error while loading. Please reload this page.
First Check
Example Code
Description
Q1,
app.py
from sqlmodel import Session
from .database import create_db_and_tables, engine
from .models import Hero, Team
def create_heroes():
with Session(engine) as session:
team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar")
def main():
create_db_and_tables()
create_heroes()
if name == "main":
main()
execute...
/Users/hongjunjie01/my-fastapi-project/venv/bin/python /Users/hongjunjie01/my-fastapi-project/app.py
Traceback (most recent call last):
File "/Users/hongjunjie01/my-fastapi-project/app.py", line 3, in
from .database import create_db_and_tables, engine
ImportError: attempted relative import with no known parent package
Process finished with exit code 1
Q2
init.py
from .models import Hero, Team
from .database import engine
execute...
/Users/hongjunjie01/my-fastapi-project/venv/bin/python /Users/hongjunjie01/my-fastapi-project/init.py
Traceback (most recent call last):
File "/Users/hongjunjie01/my-fastapi-project/init.py", line 1, in
from .models import Hero, Team
ImportError: attempted relative import with no known parent package
Process finished with exit code 1
Operating System
macOS
Operating System Details
Macmini m6 16GB with macos.
Project Version
Fastapi(learning)
Python Version
Python 3.15.0
Additional Context
I step by step to learn with Tutorial - User Guide.
But when I learn this chapter and find some error that I can't find problem.Code Structure and Multiple Files
All reactions