strawberry
strawberry copied to clipboard
When i use run with python3, ImportError occured
ImportError : cannot import name 'GraphQLError' from 'graphql'
Describe the Bug
It works well when executed with poetry run app.main:main. However, when executing with python3 app/main.py, the following Import Error occurs.
Error occured code line
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)
System Information
- Operating System: Mac Ventura 13.5.1(22G90)
- Strawberry Version (if applicable): Entered in pyproject.toml as follows:
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"
Additional Context
- I already run 'Invaidate Caches` in pycharm.
Upvote & Fund
- We're using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.
I found anwser.
in strawberry module import graphql.
strawberry/exceptions/init.py
...
from graphql import GraphQLError
...
but my project already same name directory like this:
βββ graphql
βββ __init__.py
because of my awful python skill. π
how can i avoid? need change my graphql directory name?
plz help me!
Can I work on this issue?
@evan-hwang Hey there!
Firstly, there's never such a thing as "awful Python skill". It's all about learning and growing!
Regarding your issue, the way Python's import system works is by prioritizing the first occurrence it finds in sys.path list. So it is likely that your graphql module appears before the actual graphql module.
If you have trouble doing these, I'd recommend renaming your module. It's generally a good practice to avoid naming your modules/packages same as those of standard/most used libraries to avoid any clashes. Alternatively you can put you graphql module in a submodule, e.g. a folder structure of your_app_name/graphql
and then run your_app_name
Erik
@SarthakNikhal I don't think there's anything to work on here from our side.
@erikwrede
Eric, your exceptionally gracious responses have deeply touched me.
While I haven't fully comprehended the latter approach you mentioned, I have opted to change the name of my directory using the former method.
Thank you!