Is it possible to check for Production or Development mode? #11847
-
DescriptionThe api = FastAPI()
if api.DevelopmentMode:
print("Running in Development mode")
else:
print("Running in Production mode") I couldn't find anything related in the FastAPI class reference. Operating SystemmacOS FastAPI Version0.111.0 Pydantic Version2.7.3 Python Version3.12.3 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
FastAPI CLI doesn't pass this information to FastAPI app. The only difference between these commands is that FastAPI CLI uses different defaults for parameters to run ASGI server ( And since it's only default parameters, they can be overridden. For example command As I know the common way to determine whether the app is running in prod or dev environment is to use ENV variable.
Then read this variable in your app:
|
Beta Was this translation helpful? Give feedback.
-
You can simply check check what command has been used using system library
output: |
Beta Was this translation helpful? Give feedback.
FastAPI CLI doesn't pass this information to FastAPI app.
The only difference between these commands is that FastAPI CLI uses different defaults for parameters to run ASGI server (
host
address andreload
).And since it's only default parameters, they can be overridden. For example command
fastapi run main.py --host 127.0.0.1 --reload
is a full equivalent offastapi dev main.py
.As I know the common way to determine whether the app is running in prod or dev environment is to use ENV variable.
Just set ENV variable with the name like
RUN_MODE
toDEV
on your local machine and toPROD
on your production serverThen read this variable in your app: