question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access a variable or function in main.py from another file.

See original GitHub issue

First 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:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
jgould22commented, Dec 15, 2021

If you place __init__.py files in the correct folders you can simply import your function with something like from <project_name>.main import rate_limit_decorator

0reactions
ifamirhasancommented, Dec 16, 2021

You can do it, create an dependencies.py (or any name you want), import it in your main (imports are made on the top of the file, and you can follow what @jgould22 said or search about “python import variable from another file” on Google) and use it in the line after the import.

I will try to make a generic example: (basic from the first link I send, but mixed with first steps)

The structure of files would be like that: (init.py is a blank file)

├── app
│   ├── __init__.py
│   ├── main.py
│   └── dependencies.py

Your main file:

from fastapi import FastAPI
# I think the standard name in Python would be rate_limiter, if it's a function and RateLimiter if it a class
from ..dependencies import rateLimiter  

app = FastAPI()


# Use it where you need
@app.get("/")
@rateLimiter  # Your decorator
async def root():
    return {"message": "Hello World"}

If I understood correctly, it is what you want, but without code and directory structure, is hard to say and I’m not an expert.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found