[QUESTION] How do I pass environment variables to my route files?
See original GitHub issueFrom the example given here - https://fastapi.tiangolo.com/tutorial/bigger-applications/
I have this kind of structure.
.
├── app
│ ├── __init__.py
│ ├── main.py
│ └── routers
│ ├── __init__.py
│ ├── items.py
│ └── users.py
I would like to read environment variables, when running/deploying my code, and then pass those variables to items.py
and users.py
How do I achieve this?
Do I have to use tags
or is that for some other purpose?
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Laravel, ENV Variable from routes file - Stack Overflow
The reason is I want to make special routes group for dashboard / client area. Here is my latest code; $dashboard = env('DASHBOARD_DOMAIN',...
Read more >Working with Environment Variables in Node.js - Twilio
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
Read more >What are PATH and other environment variables, and how ...
To do so, right click on the desktop, select New Shortcut, and enter systempropertiesadvanced.exe. Then you can click on the link to get...
Read more >Environment variables in Compose | Docker Documentation
You can set default values for environment variables using a .env file, which Compose automatically looks for in project directory (parent folder of...
Read more >Node.js Everywhere with Environment Variables! - Medium
The following command will preload all environment variables from the file . env using dotenv and make them available in your app. So...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@unography I would recommend the use of the
BaseSettings
class in pydantic. Here’s a basic example:This way you can basically treat the settings like a singleton by only accessing them via
get_settings()
, with the benefit that you can modify the environment after module imports but prior to the first call toget_settings
, and the settings will reflect the environment on its first call. (This may be useful for testing purposes.)I just wanted to add a further approach that you may wish to take. My approach is more similar to Flask and Django; it depends on environment variables mainly for knowing the path of your settings file.
I’ve chosen to use TOML, but you can use YAML or JSON too.
My
settings.py
file looks like this:It is also easy to point your unit tests to your testing config as long as you don’t attempt to use
get_settings
too early.If you are in a situation where this causes you trouble, you may wrap your app in a
get_app
function which is called similarly to that below inmain.py
:This way, importing the
main
module in your unit tests won’t create the app and you are free to set the appropriate environment variable before callingget_app
yourself 😄Note: In case anyone is unaware, it seems impossible to call a function when running gunicorn or uvicorn like you can with sync workers. As such, the
main
module must define anapp
variable itself which is why we need that litle trick above.The best way to avoid this problem is to use the
startup
event handler for anything that must happen upon startup that needs to access settings. Thestartup
event won’t run immediately when the app is imported in your unit tests; only once theTestClient
starts using it;.Really hope this helps and provides some more ideas which we can develop further together.
Cheers Fotis