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

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

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

问题

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

错误:

  1. PS C:\Users\obunga\OneDrive\Desktop\py\fastfast> uvicorn main:app --reload
  2. INFO: 将监视这些目录中的更改: ['C:\\Users\\obunga\\OneDrive\\Desktop\\py\\fastfast']
  3. INFO: Uvicorn运行在http://127.0.0.1:8000(按CTRL+C退出)
  4. INFO: 启动重载器进程[5464],使用WatchFiles
  5. Process SpawnProcess-1:
  6. Traceback(最近一次调用最近):
  7. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py",第314行,_bootstrap
  8. self.run()
  9. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py",第108行,run
  10. self._target(*self._args,**self._kwargs)
  11. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\_subprocess.py",第76行,subprocess_started
  12. target(sockets=sockets)
  13. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py",第60行,run
  14. return asyncio.run(self.serve(sockets=sockets))
  15. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  16. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py",第190行,run
  17. return runner.run(main)
  18. ^^^^^^^^^^^^^^^^
  19. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py",第118行,run
  20. return self._loop.run_until_complete(task)
  21. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  22. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py",第653行,run_until_complete
  23. return future.result()
  24. ^^^^^^^^^^^^^^^
  25. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py",第67行,serve
  26. config.load()
  27. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\config.py",第477行,load
  28. self.loaded_app = import_from_string(self.app)
  29. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  30. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\importer.py",第21行,import_from_string
  31. module = importlib.import_module(module_str)
  32. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  33. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py",第126行,import_module
  34. return _bootstrap._gcd_import(name[level:],packagelevel)
  35. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  36. File "<frozen importlib._bootstrap>",第1206行,_gcd_import
  37. File "<frozen importlib._bootstrap>",第1178行,_find_and_load
  38. File "<frozen importlib._bootstrap>",第1149行,_find_and_load_unlocked
  39. File "<frozen importlib._bootstrap>",第690行,_load_unlocked
  40. File "<frozen importlib._bootstrap_external>",第936行,exec_module
  41. File "<frozen importlib._bootstrap_external>",第1074行,get_code
  42. File "<frozen importlib._bootstrap_external>",第1004行,source_to_code
  43. File "<frozen importlib._bootstrap>",第241行,_call_with_frames_removed
  44. ValueError: 源代码字符串不能包含空字节

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

代码非常基本:main.py -

  1. from fastapi import FastAPI
  2. app = FastAPI()
  3. @app.get("/")
  4. def index():
  5. return {"lyrics": "heyyyyyyyyy"}

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

问题是什么,如何修复?

英文:

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

Error:

  1. PS C:\Users\obunga\OneDrive\Desktop\py\fastfast> uvicorn main:app --reload
  2. INFO: Will watch for changes in these directories: ['C:\\Users\\obunga\\OneDrive\\Desktop\\py\\fastfast']
  3. INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
  4. INFO: Started reloader process [5464] using WatchFiles
  5. Process SpawnProcess-1:
  6. Traceback (most recent call last):
  7. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py", line 314, in _bootstrap
  8. self.run()
  9. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py", line 108, in run
  10. self._target(*self._args, **self._kwargs)
  11. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started
  12. target(sockets=sockets)
  13. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 60, in run
  14. return asyncio.run(self.serve(sockets=sockets))
  15. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  16. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
  17. return runner.run(main)
  18. ^^^^^^^^^^^^^^^^
  19. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
  20. return self._loop.run_until_complete(task)
  21. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  22. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
  23. return future.result()
  24. ^^^^^^^^^^^^^^^
  25. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 67, in serve
  26. config.load()
  27. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\config.py", line 477, in load
  28. self.loaded_app = import_from_string(self.app)
  29. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  30. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
  31. module = importlib.import_module(module_str)
  32. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  33. File "C:\Users\obunga\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
  34. return _bootstrap._gcd_import(name[level:], package, level)
  35. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  36. File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  37. File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  38. File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  39. File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  40. File "<frozen importlib._bootstrap_external>", line 936, in exec_module
  41. File "<frozen importlib._bootstrap_external>", line 1074, in get_code
  42. File "<frozen importlib._bootstrap_external>", line 1004, in source_to_code
  43. File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  44. 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 -

  1. from fastapi import FastAPI
  2. app = FastAPI()
  3. @app.get("/")
  4. def index():
  5. 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:

确定