rafsaf / minimal-fastapi-postgres-template

minimal-fastapi-postgres-template based on official template but rewritten
https://minimal-fastapi-postgres-template.rafsaf.pl
MIT License
464 stars 63 forks source link

Failed when execute pytest #87

Open androchentw opened 1 week ago

androchentw commented 1 week ago

Thank you for the great repo first! ⭐

I could successfully execute and see fastapi on with the following:

cd your_project_name
vi .env

### Poetry install (python3.12)
poetry install
poetry shell

### Setup database
docker-compose up -d

### Run Alembic migrations
alembic upgrade head

with WSL2 and in vs code terminal (zsh), selecting the poetry venv as Python interpreter.

However, I failed to execute pytest as below. Is there anything I'm missing?

Error msg:

❯ pytest
Traceback (most recent call last):
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/bin/pytest", line 8, in <module>
    sys.exit(console_main())
             ^^^^^^^^^^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 201, in console_main
    code = main()
           ^^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 156, in main
    config = _prepareconfig(args, plugins)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 341, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/_pytest/helpconfig.py", line 105, in pytest_cmdline_parse
    config = yield
             ^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1140, in pytest_cmdline_parse
    self.parse(args)
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1490, in parse
    self._preparse(args, addopts=addopts)
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1377, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/pluggy/_manager.py", line 421, in load_setuptools_entrypoints
    plugin = ep.load()
             ^^^^^^^^^
  File "/home/andro/.pyenv/versions/3.12.7/lib/python3.12/importlib/metadata/__init__.py", line 205, in load
    module = import_module(match.group('module'))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andro/.pyenv/versions/3.12.7/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/_pytest/assertion/rewrite.py", line 174, in exec_module
    exec(co, module.__dict__)
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/pytest_cov/plugin.py", line 9, in <module>
    import coverage
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/coverage/__init__.py", line 24, in <module>
    from coverage.control import (
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/coverage/control.py", line 29, in <module>
    from coverage.collector import Collector
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/coverage/collector.py", line 21, in <module>
    from coverage.data import CoverageData
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/coverage/data.py", line 25, in <module>
    from coverage.sqldata import CoverageData
  File "/mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/lib/python3.12/site-packages/coverage/sqldata.py", line 16, in <module>
    import sqlite3
  File "/home/andro/.pyenv/versions/3.12.7/lib/python3.12/sqlite3/__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "/home/andro/.pyenv/versions/3.12.7/lib/python3.12/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
rafsaf commented 1 week ago

Hi, it looks like you don't use poetry virtualenv. Your python path is /home/andro/.pyenv/versions/3.12.7/lib/python3.12 and poetry usually creates venv in path like /home/andro/.local/...something (with all dependencies installed). Please check the exact path with poetry env info and use it instead.

BR

androchentw commented 1 week ago

Thank you so much for the prompt reply!

I'm not that familiar with poetry yet but will check details later.

I've tried something like:

❯ poetry env info

Virtualenv
Python:         3.12.7
Implementation: CPython
Path:           /mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv
Executable:     /mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/bin/python
Valid:          True

Base
Platform:   linux
OS:         posix
Python:     3.12.7
Path:       /home/andro/.pyenv/versions/3.12.7
Executable: /home/andro/.pyenv/versions/3.12.7/bin/python3.12

❯ poetry shell
Spawning shell within /mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv
❯ emulate bash -c '. /mnt/c/andro/dev/minimal-fastapi-postgres-template/.venv/bin/activate'