Access a variable or function in main.py from another file.
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
no code :(
Description
I want to get access to a variable or function which is in main.py from another file (a file which has routers). for example I create a variable (request rate limiter) in main.py and pass it to fastapi app. then I want to use it as decorator when defining routes but my routes are not in main.py so I have to pass to router file to use it while creating routes. I used this example (rate limiter) to just explain my problem and there might be better ways for setting up this and may no need for passing variable like this. Thank you in advance
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.65.2
Python Version
3.8.6
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:8
Top Results From Across the Web
How to import variables from another file in Python?
The from import Statement · import <file_name> and then use <file_name>.<variable_name> to access variable · from <file_name> import < ...
Read more >python - Access variable in main function from other script
The variable myValue is not updated in script2.py as you don't call the addition() function by importing script1.
Read more >Python import variable from another file | Example code
First import the file from the current program, then you can import the variable or access the variable in Python.
Read more >Programming FAQ — Python 3.11.1 documentation
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the...
Read more >Defining Main Functions in Python
Use if __name__ == "__main__" to Control the Execution of Your Code ... What if you want process_data() to execute when you run...
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
If you place
__init__.py
files in the correct folders you can simply import your function with something likefrom <project_name>.main import rate_limit_decorator
Thanks for your answer and help. I understand what you are saying but the rate limiter is just an example. In my case it is different and I have to do few things with that variable before app starts and I can’t create it for each request. but fortunately I figured it out, as I said in my last comment I have to create the variable in a file (config.py) and I can import it every where and use it globally. Again thanks for your help.