strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

When i use run with python3, ImportError occured

Open evan-hwang opened this issue 1 year ago β€’ 5 comments

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 image

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.
Fund with Polar

evan-hwang avatar Jan 19 '24 02:01 evan-hwang

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!

evan-hwang avatar Jan 19 '24 05:01 evan-hwang

Can I work on this issue?

SarthakNikhal avatar Jan 19 '24 18:01 SarthakNikhal

@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

erikwrede avatar Jan 20 '24 16:01 erikwrede

@SarthakNikhal I don't think there's anything to work on here from our side.

erikwrede avatar Jan 20 '24 17:01 erikwrede

@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!

evan-hwang avatar Jan 22 '24 06:01 evan-hwang