Not able to run FastAPI server, ValueError: source code string cannot contain null bytes

huangapple go评论74阅读模式
英文:

Not able to run FastAPI server, ValueError: source code string cannot contain null bytes

问题

学习FastAPI时,每当我尝试运行它时都会出现错误。

错误:

PS C:\Users\obunga\OneDrive\Desktop\py\fastfast> uvicorn main:app --reload
INFO:     将监视这些目录中的更改: ['C:\\Users\\obunga\\OneDrive\\Desktop\\py\\fastfast']
INFO:     Uvicorn运行在http://127.0.0.1:8000(按CTRL+C退出)
INFO:     启动重载器进程[5464],使用WatchFiles
Process SpawnProcess-1:
Traceback(最近一次调用最近):
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py",第314行,_bootstrap
    self.run()
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py",第108行,run
    self._target(*self._args,**self._kwargs)
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\_subprocess.py",第76行,subprocess_started
    target(sockets=sockets)
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py",第60行,run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py",第190行,run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py",第118行,run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py",第653行,run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py",第67行,serve
    config.load()
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\config.py",第477行,load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\importer.py",第21行,import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py",第126行,import_module
    return _bootstrap._gcd_import(name[level:],package,level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>",第1206行,_gcd_import
  File "<frozen importlib._bootstrap>",第1178行,_find_and_load
  File "<frozen importlib._bootstrap>",第1149行,_find_and_load_unlocked
  File "<frozen importlib._bootstrap>",第690行,_load_unlocked
  File "<frozen importlib._bootstrap_external>",第936行,exec_module
  File "<frozen importlib._bootstrap_external>",第1074行,get_code
  File "<frozen importlib._bootstrap_external>",第1004行,source_to_code
  File "<frozen importlib._bootstrap>",第241行,_call_with_frames_removed
ValueError: 源代码字符串不能包含空字节

我已经尝试重新安装Python、FastAPI和Uvicorn,但错误仍然存在。我的Python版本是3.11.2。

代码非常基本:main.py -

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def index():
    return {"lyrics": "heyyyyyyyyy"}

http://127.0.0.1:8000 无法访问。

问题是什么,如何修复?

英文:

Learning FastAPI, however, an error occurs whenever I'm trying to run it.

Error:

PS C:\Users\obunga\OneDrive\Desktop\py\fastfast> uvicorn main:app --reload
INFO:     Will watch for changes in these directories: ['C:\\Users\\obunga\\OneDrive\\Desktop\\py\\fastfast']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [5464] using WatchFiles
Process SpawnProcess-1:
Traceback (most recent call last):
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py", line 314, in _bootstrap
    self.run()
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 67, in serve
    config.load()
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\config.py", line 477, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 936, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1074, in get_code
  File "<frozen importlib._bootstrap_external>", line 1004, in source_to_code
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
ValueError: source code string cannot contain null bytes

I've tried re-installing Python, FastAPI and Uvicorn, but the error remains. My python version: 3.11.2

The code is pretty basic one: main.py -

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def index():
    return {"lyrics": "heyyyyyyyyy"}

http://127.0.0.1:8000 is unreachable.

What is the issue and how to fix it?

答案1

得分: 2

  1. 将 main.py 文件格式设置为 UTF-8。
  2. 将位于 C:\Users\USERNAME\AppData\Local\Programs\Python\Python311\Lib\importlib 下的文件格式设置为 UTF-8。
    你可以使用 VSCODE(右下角->保存为编码)Notepad++(编码->转换为UTF-8) 来将文件格式设置为 UTF-8。
英文:

After surfing the web for the solution for hours, one/both of these solutions worked. I found that this issue occurs when doing Django or PHP stuff too.

  1. Formatting the main.py file to UTF-8
  2. Formatting the files under C:\Users\USERNAME\AppData\Local\Programs\Python\Python311\Lib\importlib to UTF-8.

You can format the files to UTF-8 using VSCODE (bottom right corner->save as encoding) or Notepad++(encoding->convert to UTF-8)

huangapple
  • 本文由 发表于 2023年3月9日 20:38:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定