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

Question:

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

Error:

PS C:UsersobungaOneDriveDesktoppyfastfast> 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:UsersobungaAppDataLocalProgramsPythonPython311Libmultiprocessingprocess.py", line 314, in _bootstrap
    self.run()
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libmultiprocessingprocess.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libsite-packagesuvicorn_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libsite-packagesuvicornserver.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libasynciorunners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libasynciorunners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libasynciobase_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libsite-packagesuvicornserver.py", line 67, in serve
    config.load()
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libsite-packagesuvicornconfig.py", line 477, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libsite-packagesuvicornimporter.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersobungaAppDataLocalProgramsPythonPython311Libimportlib__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?

Asked By: ransom

||

Answers:

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:UsersUSERNAMEAppDataLocalProgramsPythonPython311Libimportlib 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)

Answered By: ransom