pythonbackendfastapi

How Do I get rid of this error:Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Program?


I keep trying to start my python backend with uvicorn main:app --reload but I keep getting this error:

INFO:     Will watch for changes in these directories: ['C:\\Users\\darkg\\OneDrive\\Desktop\\loginpage\\FastAP_BackEnd\\books']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [532] using StatReload
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
  File "C:\Users\darkg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\uvicorn\__init__.py", line 1, in <module>
    from uvicorn.config import Config
  File "C:\Users\darkg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\uvicorn\config.py", line 1, in <module>
    import asyncio
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\asyncio\__init__.py", line 8, in <module>
    from .base_events import *
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 18, in <module>
    import concurrent.futures
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\__init__.py", line 8, in <module>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\_base.py", line 7, in <module>
    import logging
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\logging\__init__.py", line 26, in <module>
    import sys, os, time, io, re, traceback, warnings, weakref, collections.abc
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\traceback.py", line 5, in <module>
    import linecache
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\linecache.py", line 11, in <module>
    import tokenize
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\tokenize.py", line 35, in <module>
    from token import *
  File "C:\Users\darkg\OneDrive\Desktop\loginpage\FastAP_BackEnd\books\.\token.py", line 5, in <module>
    from . import schemas
ImportError: attempted relative import with no known parent package

I tried starting it with python -m uvicorn main:app --reload but I get this error instead


Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "C:\Users\darkg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\uvicorn\__init__.py", line 1, in <module>
    from uvicorn.config import Config
  File "C:\Users\darkg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\uvicorn\config.py", line 1, in <module>
    import asyncio
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\asyncio\__init__.py", line 8, in <module>
    from .base_events import *
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 18, in <module>
    import concurrent.futures
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\__init__.py", line 8, in <module>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\_base.py", line 7, in <module>
    import logging
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\logging\__init__.py", line 26, in <module>
    import sys, os, time, io, re, traceback, warnings, weakref, collections.abc
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\traceback.py", line 5, in <module>
    import linecache
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\linecache.py", line 11, in <module>
    import tokenize
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0_x64__qbz5n2kfra8p0\lib\tokenize.py", line 35, in <module>
    from token import *
  File "C:\Users\darkg\OneDrive\Desktop\loginpage\FastAP_BackEnd\books\token.py", line 5, in <module>
    from . import schemas
ImportError: attempted relative import with no known parent package

Here is the main code

from . import models
from fastapi import FastAPI

from .database import engine
from .routers import books, users,authentication

app=FastAPI()
models.Base.metadata.create_all(engine)

app.include_router(books.router)
app.include_router(users.router)
app.include_router(authentication.router)



Here is my Code File Path If that helps solve the issue : Code File Structure

I have tried everything but cant seem to find a way to fix this . I've redowloaded python. I've reinstalled every package, ive added paths to my packages, but nothing seems to work. Any help would be greatly appreciated. I am completely lost


Solution

  • Python doesn't know that the directory containing your modules (including main.py) is a package, therefore it can't properly locate imports. Create an empty __init__.py file in this directory to mark it as a regular package.

    A rule of thumb is to mark all your directories that contain python modules as packages with this method.

    There's another type of Python packages that may make your code sometimes work: namespace packages. They don't have to contain an __init__.py file. I recommend to just avoid them, unless you understand exactly what they're for and why you need them. [Doc reference]

    For a quick reference, search through the FastAPI documentation for "project file structure" (it may be called something similar).