visini / abstracting-fastapi-services

FastAPI application and service structure for a more maintainable codebase – See full article
https://camillovisini.com/coding/abstracting-fastapi-services
MIT License
386 stars 54 forks source link

`make dev` throws `[FileNotFoundError] [Errno 2] No such file or directory` #2

Closed heshdotcc closed 3 years ago

heshdotcc commented 3 years ago

Edit: The following command...

poetry -vvv run uvicorn main:app --reload

throws:

[NoSuchOptionException]
The "--reload" option does not exist.

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/clikit/console_application.py", line 123, in run
    io = io_factory(
  File "/usr/lib/python3.9/site-packages/poetry/console/config/application_config.py", line 168, in create_io
    resolved_command = application.resolve_command(args)
  File "/usr/lib/python3.9/site-packages/clikit/console_application.py", line 110, in resolve_command
    return self._config.command_resolver.resolve(args, self)
  File "/usr/lib/python3.9/site-packages/clikit/resolver/default_resolver.py", line 43, in resolve
    result = self.process_default_commands(args, application.default_commands)
  File "/usr/lib/python3.9/site-packages/clikit/resolver/default_resolver.py", line 104, in process_default_commands
    if resolved_command.is_parsable():
  File "/usr/lib/python3.9/site-packages/clikit/resolver/resolve_result.py", line 43, in is_parsable
    self._parse()
  File "/usr/lib/python3.9/site-packages/clikit/resolver/resolve_result.py", line 49, in _parse
    self._parsed_args = self._command.parse(self._raw_args)
  File "/usr/lib/python3.9/site-packages/clikit/api/command/command.py", line 113, in parse
    return self._config.args_parser.parse(args, self._args_format, lenient)
  File "/usr/lib/python3.9/site-packages/clikit/args/default_args_parser.py", line 53, in parse
    self._parse(args, _fmt, lenient)
  File "/usr/lib/python3.9/site-packages/clikit/args/default_args_parser.py", line 101, in _parse
    self._parse_long_option(token, tokens, fmt, lenient)
  File "/usr/lib/python3.9/site-packages/clikit/args/default_args_parser.py", line 247, in _parse_long_option
    self._add_long_option(name, None, tokens, fmt, lenient)
  File "/usr/lib/python3.9/site-packages/clikit/args/default_args_parser.py", line 300, in _add_long_option
    raise NoSuchOptionException(name)

And regarding the environment... poetry env info throws:

Virtualenv
Python:         3.9.1
Implementation: CPython
Path:           ~/.cache/pypoetry/virtualenvs/fastapi-service-2hjJIUGa-py3.9
Valid:          True

System
Platform: linux
OS:       posix
Python:   /usr
visini commented 3 years ago

Hi @fchpr, can you please provide full stacktrace? Thank you!

heshdotcc commented 3 years ago

Feel free to ask me anything else, and thanks to you for such quick response...

visini commented 3 years ago

Can you try if this resolves both your issues (FileNotFoundError originally included in this issue, and the Traceback you added above for NoSuchOptionException).

  1. Make sure you are in the same folder as pyproject.toml
  2. Execute the following commands:
poetry env remove python              # this will delete virtualenv
poetry                                # this will install all dependencies (again)
cd app                                # this will navigate to the folder where the FastAPI app is located
poetry run uvicorn main:app --reload  # this will run uvicorn with hot reloading
heshdotcc commented 3 years ago

Now it throws:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [11076] using statreload
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 "/usr/lib/python3.9/site-packages/uvicorn/subprocess.py", line 61, in subprocess_started
    target(sockets=sockets)
  File "/usr/lib/python3.9/site-packages/uvicorn/server.py", line 48, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "/usr/lib/python3.9/site-packages/uvicorn/server.py", line 55, in serve
    config.load()
  File "/usr/lib/python3.9/site-packages/uvicorn/config.py", line 308, in load
    self.loaded_app = import_from_string(self.app)
  File "/usr/lib/python3.9/site-packages/uvicorn/importer.py", line 23, in import_from_string
    raise exc from None
  File "/usr/lib/python3.9/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "./main.py", line 4, in <module>
    from routers import foo
  File "./routers/foo.py", line 3, in <module>
    from services.foo import FooService
  File "./services/foo.py", line 6, in <module>
    from utils.service_result import ServiceResult
  File "./utils/service_result.py", line 1, in <module>
    from loguru import logger
ModuleNotFoundError: No module named 'loguru'

It seems an issue related to virtualenv, maybe since I had upgraded the system (now it's running on Python 3.9).

heshdotcc commented 3 years ago

This seems to be an specific issue related to my system. Worked in other environment.