s3rius / FastAPI-template

Feature rich robust FastAPI template.
MIT License
1.83k stars 161 forks source link

New project base on FastAPI-template got errors when running on Linux #130

Closed djun closed 1 year ago

djun commented 1 year ago

New project base on FastAPI-template got errors when running on Linux.

I've tested on VPS or local virtual machines, and on Ubuntu 18.04 or ParrotOS Home 5.1 (based on Debian), and on local Python 3.9, Miniconda3 Python 3.9 , even on building Python 3.9 slim project image but not in built Python 3.9 slim project image.

All have got this similar error when running poetry run python -m <My Project Name> (or /usr/local/bin/python -m <My Project Name> in docker image) : importlib.metadata.PackageNotFoundError: <My Project Name>.

By the way, all is well when testing on Windows (Official Python 3.9 installer).

Detail log (on ParrotOS Home 5.1 (based on Debian)):

┌─[parrot@parrot-vmware71]─[~/Desktop/demo1]
└──╼ $ls
demo1  deploy  Dockerfile  poetry.lock  pyproject.toml  README.md
┌─[parrot@parrot-vmware71]─[~/Desktop/demo1]
└──╼ $poetry run python -m demo1
INFO:     Will watch for changes in these directories: ['/home/parrot/Desktop/demo1']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [4079] using WatchFiles
Process SpawnProcess-1:
Traceback (most recent call last):
  File "/usr/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/lib/python3.9/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/parrot/.cache/pypoetry/virtualenvs/demo1-VzOo4Qa6-py3.9/lib/python3.9/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/home/parrot/.cache/pypoetry/virtualenvs/demo1-VzOo4Qa6-py3.9/lib/python3.9/site-packages/uvicorn/server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/home/parrot/.cache/pypoetry/virtualenvs/demo1-VzOo4Qa6-py3.9/lib/python3.9/site-packages/uvicorn/server.py", line 67, in serve
    config.load()
  File "/home/parrot/.cache/pypoetry/virtualenvs/demo1-VzOo4Qa6-py3.9/lib/python3.9/site-packages/uvicorn/config.py", line 485, in load
    self.loaded_app = self.loaded_app()
  File "/home/parrot/Desktop/demo1/demo1/web/application.py", line 30, in get_app
    version=metadata.version("demo1"),
  File "/usr/lib/python3.9/importlib/metadata.py", line 551, in version
    return distribution(distribution_name).version
  File "/usr/lib/python3.9/importlib/metadata.py", line 524, in distribution
    return Distribution.from_name(distribution_name)
  File "/usr/lib/python3.9/importlib/metadata.py", line 187, in from_name
    raise PackageNotFoundError(name)
importlib.metadata.PackageNotFoundError: demo1

I haven't got this error in September in this year, all was normal. But got this headache error in this month.

Please help me for this for my production environment, thanks very much!

s3rius commented 1 year ago

Hi. I need a little bit more context. Did you modify your Docker image? Maybe you deleted second poetry install at the end of the Dockerfile.

Add this before CMD command in your Dockerfile:

RUN poetry install

Also it's not that critical to delete this line from the main module.

        version=metadata.version("{{cookiecutter.project_name}}"), # Just delete it. It will solve the issue.
s3rius commented 1 year ago

The point here to run poetry install twice.

During the first run it installs all the dependencies, during the second run it installs your project.

djun commented 1 year ago

Thanks for your reply! I will try your advices in the next day.