ImportError : cannot import name 'GraphQLError' from 'graphql'
It works well when executed with poetry run app.main:main.
However, when executing with python3 app/main.py, the following Import Error occurs.
Traceback
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/evanhwang/dev/ai-hub/hub-api/app/bootstrap/admin/bootstrapper.py", line 4, in <module>
from app.bootstrap.admin.router import AdminRouter
File "/Users/evanhwang/dev/ai-hub/hub-api/app/bootstrap/admin/router.py", line 3, in <module>
import strawberry
File "/Users/evanhwang/Library/Caches/pypoetry/virtualenvs/hub-api-UM7sgzi1-py3.11/lib/python3.11/site-packages/strawberry/__init__.py", line 1, in <module>
from . import experimental, federation, relay
File "/Users/evanhwang/Library/Caches/pypoetry/virtualenvs/hub-api-UM7sgzi1-py3.11/lib/python3.11/site-packages/strawberry/federation/__init__.py", line 1, in <module>
from .argument import argument
File "/Users/evanhwang/Library/Caches/pypoetry/virtualenvs/hub-api-UM7sgzi1-py3.11/lib/python3.11/site-packages/strawberry/federation/argument.py", line 3, in <module>
from strawberry.arguments import StrawberryArgumentAnnotation
File "/Users/evanhwang/Library/Caches/pypoetry/virtualenvs/hub-api-UM7sgzi1-py3.11/lib/python3.11/site-packages/strawberry/arguments.py", line 18, in <module>
from strawberry.annotation import StrawberryAnnotation
File "/Users/evanhwang/Library/Caches/pypoetry/virtualenvs/hub-api-UM7sgzi1-py3.11/lib/python3.11/site-packages/strawberry/annotation.py", line 23, in <module>
from strawberry.custom_scalar import ScalarDefinition
File "/Users/evanhwang/Library/Caches/pypoetry/virtualenvs/hub-api-UM7sgzi1-py3.11/lib/python3.11/site-packages/strawberry/custom_scalar.py", line 19, in <module>
from strawberry.exceptions import InvalidUnionTypeError
File "/Users/evanhwang/Library/Caches/pypoetry/virtualenvs/hub-api-UM7sgzi1-py3.11/lib/python3.11/site-packages/strawberry/exceptions/__init__.py", line 6, in <module>
from graphql import GraphQLError
ImportError: cannot import name 'GraphQLError' from 'graphql' (/Users/evanhwang/dev/ai-hub/hub-api/app/graphql/__init__.py)
strawberry-graphql = {extras = ["debug-server", "fastapi"], version = "^0.217.1"}
pyproject.toml
##############################################################################
# poetry ์ข
์์ฑ ์ค์
# - https://python-poetry.org/docs/managing-dependencies/#dependency-groups
# - ๊ธฐ๋ณธ์ ์ผ๋ก PyPI์์ ์ข
์์ฑ์ ์ฐพ์ต๋๋ค.
##############################################################################
[tool.poetry.dependencies]
python = "3.11.*"
fastapi = "^0.103.2"
uvicorn = "^0.23.2"
poethepoet = "^0.24.0"
requests = "^2.31.0"
poetry = "^1.6.1"
sqlalchemy = "^2.0.22"
sentry-sdk = "^1.32.0"
pydantic-settings = "^2.0.3"
psycopg2-binary = "^2.9.9"
cryptography = "^41.0.4"
python-ulid = "^2.2.0"
ulid = "^1.1"
redis = "^5.0.1"
aiofiles = "^23.2.1"
pyyaml = "^6.0.1"
python-jose = "^3.3.0"
strawberry-graphql = {extras = ["debug-server", "fastapi"], version = "^0.217.1"}
[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
pytest-mock = "^3.6.1"
httpx = "^0.24.1"
poetry = "^1.5.1"
sqlalchemy = "^2.0.22"
redis = "^5.0.1"
mypy = "^1.7.0"
types-aiofiles = "^23.2.0.0"
types-pyyaml = "^6.0.12.12"
commitizen = "^3.13.0"
black = "^23.3.0" # fortmatter
isort = "^5.12.0" # import ์ ๋ ฌ
pycln = "^2.1.5" # unused import ์ ๋ฆฌ
ruff = "^0.0.275" # linting
##############################################################################
# poethepoet
# - https://github.com/nat-n/poethepoet
# - poe๋ฅผ ํตํ ํ์คํฌ ๋ฌ๋ ์ค์
##############################################################################
types-requests = "^2.31.0.20240106"
pre-commit = "^3.6.0"
[tool.poe.tasks.format-check-only]
help = "Check without formatting with 'pycln', 'black', 'isort'."
sequence = [
{cmd = "pycln --check ."},
{cmd = "black --check ."},
{cmd = "isort --check-only ."}
]
[tool.poe.tasks.format]
help = "Run formatter with 'pycln', 'black', 'isort'."
sequence = [
{cmd = "pycln -a ."},
{cmd = "black ."},
{cmd = "isort ."}
]
[tool.poe.tasks.lint]
help = "Run linter with 'ruff'."
cmd = "ruff ."
[tool.poe.tasks.type-check]
help = "Run type checker with 'mypy'"
cmd = "mypy ."
[tool.poe.tasks.clean]
help = "Clean mypy_cache, pytest_cache, pycache..."
cmd = "rm -rf .coverage .mypy_cache .pytest_cache **/__pycache__"
##############################################################################
# isort
# - https://pycqa.github.io/isort/
# - python import ์ ๋ ฌ ๋ชจ๋ ์ค์
##############################################################################
[tool.isort]
profile = "black"
##############################################################################
# ruff
# - https://github.com/astral-sh/ruff
# - Rust ๊ธฐ๋ฐ ํฌ๋งทํฐ, ๋ฆฐํฐ์
๋๋ค.
##############################################################################
[tool.ruff]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"C", # flake8-comprehensions
"B", # flake8-bugbear
# "T20", # flake8-print
]
ignore = [
"E501", # line too long, handled by black
"E402", # line too long, handled by black
"B008", # do not perform function calls in argument defaults
"C901", # too complex
]
[tool.commitizen]
##############################################################################
# mypy ์ค์
# - https://mypy.readthedocs.io/en/stable/
# - ์ ์ ํ์
์ฒดํฌ๋ฅผ ์ํํฉ๋๋ค.
##############################################################################
[tool.mypy]
python_version = "3.11"
packages=["app"]
exclude=["tests"]
ignore_missing_imports = true
show_traceback = true
show_error_codes = true
disable_error_code="misc, attr-defined"
follow_imports="skip"
#strict = false
# ๋ค์์ --strict์ ํฌํจ๋ ์ฌ๋ฌ ์ต์
๋ค์
๋๋ค.
warn_unused_configs = true # mypy ์ค์ ์์ ์ฌ์ฉ๋์ง ์์ [mypy-<pattern>] config ์น์
์ ๋ํด ๊ฒฝ๊ณ ๋ฅผ ๋ฐ์์ํต๋๋ค. (์ฆ๋ถ ๋ชจ๋๋ฅผ ๋๋ ค๋ฉด --no-incremental ์ฌ์ฉ ํ์)
disallow_any_generics = false # ๋ช
์์ ์ธ ํ์
๋งค๊ฐ๋ณ์๋ฅผ ์ง์ ํ์ง ์์ ์ ๋ค๋ฆญ ํ์
์ ์ฌ์ฉ์ ๊ธ์งํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋จ์ํ x: list์ ๊ฐ์ ์ฝ๋๋ ํ์ฉ๋์ง ์์ผ๋ฉฐ ํญ์ x: list[int]์ ๊ฐ์ด ๋ช
์์ ์ผ๋ก ์์ฑํด์ผ ํฉ๋๋ค.
disallow_subclassing_any = true # ํด๋์ค๊ฐ Any ํ์
์ ์์ํ ๋ ์ค๋ฅ๋ฅผ ๋ณด๊ณ ํฉ๋๋ค. ์ด๋ ๊ธฐ๋ณธ ํด๋์ค๊ฐ ์กด์ฌํ์ง ์๋ ๋ชจ๋์์ ๊ฐ์ ธ์ฌ ๋( --ignore-missing-imports ์ฌ์ฉ ์) ๋๋ ๊ฐ์ ธ์ค๊ธฐ ๋ฌธ์ # type: ignore ์ฃผ์์ด ์๋ ๊ฒฝ์ฐ์ ๋ฐ์ํ ์ ์์ต๋๋ค.
disallow_untyped_calls = true # ํ์
์ด๋
ธํ
์ด์
์ด ์๋ ํจ์ ์ ์์์ ํจ์ ํธ์ถ์ ์ค๋ฅ๋ฅผ ๋ณด๊ณ ํฉ๋๋ค.
disallow_untyped_defs = false # ํ์
์ด๋
ธํ
์ด์
์ด ์๊ฑฐ๋ ๋ถ์์ ํ ํ์
์ด๋
ธํ
์ด์
์ด ์๋ ํจ์ ์ ์๋ฅผ ๋ณด๊ณ ํฉ๋๋ค. (--disallow-incomplete-defs์ ์์ ์งํฉ)
disallow_incomplete_defs = false # ๋ถ๋ถ์ ์ผ๋ก ์ฃผ์์ด ๋ฌ๋ฆฐ ํจ์ ์ ์๋ฅผ ๋ณด๊ณ ํฉ๋๋ค. ๊ทธ๋ฌ๋ ์์ ํ ์ฃผ์์ด ๋ฌ๋ฆฐ ์ ์๋ ์ฌ์ ํ ํ์ฉ๋ฉ๋๋ค.
check_untyped_defs = true # ํ์
์ด๋
ธํ
์ด์
์ด ์๋ ํจ์์ ๋ณธ๋ฌธ์ ํญ์ ํ์
์ฒดํฌํฉ๋๋ค. (๊ธฐ๋ณธ์ ์ผ๋ก ์ฃผ์์ด ์๋ ํจ์์ ๋ณธ๋ฌธ์ ํ์
์ฒดํฌ๋์ง ์์ต๋๋ค.) ๋ชจ๋ ๋งค๊ฐ๋ณ์๋ฅผ Any๋ก ๊ฐ์ฃผํ๊ณ ํญ์ Any๋ฅผ ๋ฐํ๊ฐ์ผ๋ก ์ถ์ ํฉ๋๋ค.
disallow_untyped_decorators = true # ํ์
์ด๋
ธํ
์ด์
์ด ์๋ ๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ ๋ ์ค๋ฅ๋ฅผ ๋ณด๊ณ ํฉ๋๋ค.
warn_redundant_casts = true # ์ฝ๋๊ฐ ๋ถํ์ํ ์บ์คํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ค๋ฅ๋ฅผ ๋ณด๊ณ ํฉ๋๋ค. ์บ์คํธ๊ฐ ์์ ํ๊ฒ ์ ๊ฑฐ๋ ์ ์๋ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ๊ฐ ๋ฐ์ํฉ๋๋ค.
warn_unused_ignores = false # ์ฝ๋์ ์ค์ ๋ก ์ค๋ฅ ๋ฉ์์ง๋ฅผ ์์ฑํ์ง ์๋ # type: ignore ์ฃผ์์ด ์๋ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ๋ฅผ ๋ฐ์์ํต๋๋ค.
warn_return_any = false # Any ํ์
์ ๋ฐํํ๋ ํจ์์ ๋ํด ๊ฒฝ๊ณ ๋ฅผ ๋ฐ์์ํต๋๋ค.
no_implicit_reexport = true # ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋์ ๊ฐ์ ธ์จ ๊ฐ์ ๋ด๋ณด๋ด์ง ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋์ด mypy๋ ๋ค๋ฅธ ๋ชจ๋์์ ์ด๋ฅผ ๊ฐ์ ธ์ค๋๋ก ํ์ฉํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด ํ๋๊ทธ๋ฅผ ์ฌ์ฉํ๋ฉด from-as๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ __all__์ ํฌํจ๋์ง ์์ ๊ฒฝ์ฐ ๋ด๋ณด๋ด์ง ์๋๋ก ๋์์ ๋ณ๊ฒฝํฉ๋๋ค.
strict_equality = true # mypy๋ ๊ธฐ๋ณธ์ ์ผ๋ก 42 == 'no'์ ๊ฐ์ ํญ์ ๊ฑฐ์ง์ธ ๋น๊ต๋ฅผ ํ์ฉํฉ๋๋ค. ์ด ํ๋๊ทธ๋ฅผ ์ฌ์ฉํ๋ฉด ์ด๋ฌํ ๋น๊ต๋ฅผ ๊ธ์งํ๊ณ ๋น์ทํ ์๋ณ ๋ฐ ์ปจํ
์ด๋ ํ์ธ์ ๋ณด๊ณ ํฉ๋๋ค. (์: from typing import Text)
extra_checks = true # ๊ธฐ์ ์ ์ผ๋ก๋ ์ฌ๋ฐ๋ฅด์ง๋ง ์ค์ ์ฝ๋์์ ๋ถํธํ ์ ์๋ ์ถ๊ฐ์ ์ธ ๊ฒ์ฌ๋ฅผ ํ์ฑํํฉ๋๋ค. ํนํ TypedDict ์
๋ฐ์ดํธ์์ ๋ถ๋ถ ์ค์ฒฉ์ ๊ธ์งํ๊ณ Concatenate๋ฅผ ํตํด ์์น ์ ์ฉ ์ธ์๋ฅผ ๋ง๋ญ๋๋ค.
# pydantic ํ๋ฌ๊ทธ์ธ ์ค์ ํ์ง ์์ผ๋ฉด ๊ฐ์ง ํ์
์ค๋ฅ ๋ฐ์ ์ฌ์ง ์์
# - https://www.twoistoomany.com/blog/2023/04/12/pydantic-mypy-plugin-in-pyproject/
plugins = ["pydantic.mypy", "strawberry.ext.mypy_plugin"]
##############################################################################
# ๋น๋ ์์คํ
์ค์
##############################################################################
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
##############################################################################
# virtualenv ์ค์
# - ํ๋ก์ ํธ์์ peotry ๋ช
๋ น ํธ์ถ ์ venv๊ฐ ์๋ค๋ฉด '.venv' ๊ฒฝ๋ก์ ์์ฑ
##############################################################################
[virtualenvs]
create = true
in-project = true
path = ".venv"
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too