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?
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.
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)