我一直尝试用
uvicorn main:app --reload
启动我的python后端,但我不断收到此错误:
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
我尝试用
python -m uvicorn main:app --reload
启动它,但我收到此错误
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
这是主要代码
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)
这是我的代码文件路径如果这有助于解决问题: 代码文件结构
我已经尝试了一切,但似乎找不到解决此问题的方法。我已经重新下载了python。我重新安装了每个软件包,添加了软件包的路径,但似乎没有任何效果。任何帮助将不胜感激。我完全迷失了
错误信息“
ImportError: attempted relative import with no known parent package
”表明在尝试执行相对导入时遇到了问题,而 Python 找不到父包。这通常发生在尝试从一个应该作为包的一部分运行的脚本中直接运行脚本时。
具体来说,问题出在的“
token.py
”文件中,该文件具有以下行:
from . import schemas
此行尝试从当前包中相对导入“
schemas
”模块。但是,由于正在直接运行“
main.py
”(即使是通过 uvicorn),所以 Python 并没有将其视为包的一部分,因此无法解析相对导入。
以下是可以解决此问题的两种方法:
1. 使用绝对导入:
将“
token.py
”中的导入语句更改为使用绝对导入:
python
from books import schemas
这将明确告诉 Python 从“
books
”包中导入“
schemas
”模块。
2. 将目录作为包运行:
可以通过在“
FastAP_BackEnd
”目录中创建一个名为“
__init__.py
”的空文件,将的项目目录转换为包。然后,可以使用
-m
标志运行 uvicorn,以将其作为包运行:
bash
python -m uvicorn books.main:app --reload
这将使 Python 将“
books
”目录识别为一个包,并正确解析相对导入。
请尝试这些解决方案之一,的问题应该得到解决。如果还有其他问题,请告诉我!
标签:python,backend,fastapi From: 73438374