I ran Python, FastAPI, and PostgreSQL as docker-compose, but it doesn’t work with these errors.
I don’t know why this error occurs.
Full Error Message:

learn-fastapi-web-1  | Traceback (most recent call last):
learn-fastapi-web-1  |   File "<frozen runpy>", line 198, in _run_module_as_main
learn-fastapi-web-1  |   File "<frozen runpy>", line 88, in _run_code
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/__main__.py", line 4, in <module>
learn-fastapi-web-1  |     uvicorn.main()
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
learn-fastapi-web-1  |     return self.main(*args, **kwargs)
learn-fastapi-web-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1078, in main
learn-fastapi-web-1  |     rv = self.invoke(ctx)
learn-fastapi-web-1  |          ^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
learn-fastapi-web-1  |     return ctx.invoke(self.callback, **ctx.params)
learn-fastapi-web-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 783, in invoke
learn-fastapi-web-1  |     return __callback(*args, **kwargs)
learn-fastapi-web-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/main.py", line 416, in main
learn-fastapi-web-1  |     run(
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/main.py", line 587, in run
learn-fastapi-web-1  |     server.run()
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 61, in run
learn-fastapi-web-1  |     return asyncio.run(self.serve(sockets=sockets))
learn-fastapi-web-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
learn-fastapi-web-1  |     return runner.run(main)
learn-fastapi-web-1  |            ^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
learn-fastapi-web-1  |     return self._loop.run_until_complete(task)
learn-fastapi-web-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 68, in serve
learn-fastapi-web-1  |     config.load()
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/config.py", line 467, in load
learn-fastapi-web-1  |     self.loaded_app = import_from_string(self.app)
learn-fastapi-web-1  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
learn-fastapi-web-1  |     module = importlib.import_module(module_str)
learn-fastapi-web-1  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
learn-fastapi-web-1  |     return _bootstrap._gcd_import(name[level:], package, level)
learn-fastapi-web-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
learn-fastapi-web-1  |   File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
learn-fastapi-web-1  |   File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
learn-fastapi-web-1  |   File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
learn-fastapi-web-1  |   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
learn-fastapi-web-1  |   File "<frozen importlib._bootstrap_external>", line 940, in exec_module
learn-fastapi-web-1  |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
learn-fastapi-web-1  |   File "/main.py", line 5, in <module>
learn-fastapi-web-1  |     from . import crud, models, schemas
learn-fastapi-web-1  | ImportError: attempted relative import with no known parent package

Dockerfile:

FROM python:latest
COPY . .
RUN pip install -r requirements.txt
CMD python -m uvicorn --host=0.0.0.0 main:app
  • Renacles@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    1 year ago

    It’s a bit hard to figure out the issue with just the stack trace but I’ll give it a shot.

    You are using relative imports and it looks like all your files are in the root folder. I suggest using only absolute imports as a rule and importing only the parts of the code that you need instead of entire files.

    So, instead of doing:

    from . import crud, etc, etc

    You can do:

    from crud import [whatever you need] from models import [whatever you need] Etc

    What is important is that you understand what the pythonpath is and how to structure your code around it.

      • Renacles@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        1 year ago

        No problem, glad to hear!

        You might want to consider putting all your python code into a ‘src’ folder, it’s a good practice to have only the files you need inside the docker container and that makes it so you can just build it with the entire folder.

        Outside of the src folder you’d keep your docker files, dependencies and such.

  • evanbung@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    This error occurs when you try to import a module using a relative import, but Python cannot find the module’s parent package.A relative import is a way to import a module that is located in the same package or module as the importing module.

    To fix this error “Attempted relative import with no known parent package”, ensure you have a proper package structure in your project, which includes an init.py file in each directory you want to treat as a package. Verify that the directory containing your main script (the one you run) is included in the Python path (sys.path). If you prefer not to use packages, consider using absolute imports by specifying the full path to the module you want to import. Alternatively, reorganize your project structure to adhere to a clear package structure for relative imports to work correctly.