0

I am using FastAPI and it works fine. But I want to use python some.py to do something extra.

In that some.py I have imported app.core.config. But when I use python some.py in the project env, it always give me the error.

from app.core.config import settings
 
if __name__ == '__main__':
     print('hello')

I tried with any other file and it gives the same error.

I have already included the __init__.py file in the app and core directories. It works fine when using uvicorn to start the fastapi service.

My question is, how to run a single py file (with fastapi app module imported) manually?

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
sean k
  • 11
  • 2
  • What error are you getting? What functions do you intend to call in your framework? – MatsLindh Jul 05 '22 at 08:39
  • The error is: ModuleNotFoundError: No module named 'app'. And I want to import something from app.core.config or any other fastapi module like app.db.base. It simply does not recognize the import from app... – sean k Jul 05 '22 at 08:41
  • That will depend on whether you've added `app` to your pythonpath or not. If you're using poetry it'll be done automagically for you if you run `poetry run python some.py`, if you're using setup.py you can install the module as editable by using `pip install -e .`. Another option is to modify python's path by doing something like `sys.path.insert(1, os.path.join(sys.path[0], ".."))` at the start of `some.py` to get the path to resolve from the parent directory (if `some.py` is inside `app`) – MatsLindh Jul 05 '22 at 08:49
  • I tried the second option and it works. Before I tryied the sys.path already inluced $project/app. After I tried the code, it added another path $project/app/.. Thank you! – sean k Jul 05 '22 at 09:04
  • https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder - this is probably a good future reference for anyone coming across this question – MatsLindh Jul 05 '22 at 09:12

2 Answers2

0

Try by renaming the directory app to apps or something else and then try again.

Shapon Sheikh
  • 21
  • 1
  • 5
0

I had this same problem months ago, so you can understand with this example. And in all my research, I decided to make the structure like this to run:

#in docker
CMD ["python", "-m","uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8050"]
#in bash
python -m uvicorn app.main:app --host 0.0.0.0 --port 8050

#In app/main.py
def create_app():
"""Create the app"""
    _app = FastAPI(
        title="Project",
    )

    @_app.get("/")
    def read_root():
       """Root route"""
       return {"Ping": "Pong"}

    return _app

app = create_app()

This bash command will execute your instance in main.py (he tries to pick up the var name in this example ":app")

idylicaro
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '22 at 04:09