Skip to content

Unable to import Celery #2290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rrfirstwork opened this issue Jul 29, 2024 · 6 comments
Closed

Unable to import Celery #2290

rrfirstwork opened this issue Jul 29, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@rrfirstwork
Copy link

rrfirstwork commented Jul 29, 2024

Bug report

Unable to run mypy with django-stubs, even with ignore_missing_imports=true.
Celery is set up in the app2/ init.py file:

from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from app2.celery_script import app as celery_app


__all__ = ("celery_app",)

Error constructing plugin instance of NewSemanalDjangoPlugin

Traceback (most recent call last):
  File "/Users/rahul/.cache/pre-commit/repo3ri7flll/py_env-python3/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/rahul/.cache/pre-commit/repo3ri7flll/py_env-python3/lib/python3.9/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 100, in main
  File "mypy/main.py", line 182, in run_build
  File "mypy/build.py", line 192, in build
  File "mypy/build.py", line 237, in _build
  File "mypy/build.py", line 501, in load_plugins
  File "mypy/build.py", line 482, in load_plugins_from_config
  File "/Users/rahul/.cache/pre-commit/repo3ri7flll/py_env-python3/lib/python3.9/site-packages/mypy_django_plugin/main.py", line 71, in __init__
    self.django_context = DjangoContext(self.plugin_config.django_settings_module)
  File "/Users/rahul/.cache/pre-commit/repo3ri7flll/py_env-python3/lib/python3.9/site-packages/mypy_django_plugin/django/context.py", line 127, in __init__
    apps, settings = initialize_django(self.django_settings_module)
  File "/Users/rahul/.cache/pre-commit/repo3ri7flll/py_env-python3/lib/python3.9/site-packages/mypy_django_plugin/django/context.py", line 81, in initialize_django
    settings._setup()  # type: ignore[misc]
  File "/Users/rahul/.cache/pre-commit/repo3ri7flll/py_env-python3/lib/python3.9/site-packages/django/conf/__init__.py", line 89, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/rahul/.cache/pre-commit/repo3ri7flll/py_env-python3/lib/python3.9/site-packages/django/conf/__init__.py", line 217, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/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 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  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 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/rahul/learning/backend/app2/__init__.py", line 5, in <module>
    from app2.celery_script import app as celery_app
  File "/Users/rahul/learning/backend/app2/celery.py", line 7, in <module>
    from celery import Celery
ModuleNotFoundError: No module named 'celery'

python -c "import celery; print(celery.__version__)"
5.3.6

What's wrong

Should be able to run mypy checks without crashing.

How is that should be

System information

  • OS:
  • python version: 3.9.6
  • django version: 4.2.13
  • mypy version: 1.7.1
  • django-stubs version: 4.2.7
  • django-stubs-ext version: 4.2.7
@rrfirstwork rrfirstwork added the bug Something isn't working label Jul 29, 2024
@flaeppe
Copy link
Member

flaeppe commented Jul 29, 2024

This is a duplicate of #329

The plugin uses the Django runtime, so if the runtime crashes, the plugin/mypy crashes. It's also mentioned in the README section "mypy crashes when I run it with this plugin installed"

To fix it you need to make sure the environment you're running mypy in has celery installed and available.

@flaeppe flaeppe closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2024
@rrfirstwork
Copy link
Author

@flaeppe the env does has celery installed if i comment out mypy the application works just fine. also opening the python terminal in the env and running celery imports also work fine

@rrfirstwork
Copy link
Author

Screenshot 2024-07-30 at 12 19 06 AM

@flaeppe
Copy link
Member

flaeppe commented Jul 29, 2024

Looks like, from the traceback, that mypy runs via pre-commit. Does the pre-commit environment have celery installed and available?

@rrfirstwork
Copy link
Author

it's there in requirements.txt do i need to do anything else?

@rrfirstwork
Copy link
Author

rrfirstwork commented Jul 29, 2024

@flaeppe i have added

- repo: local
  hooks:
  - id: install-dependencies
    name: Install Dependencies
    entry: ./install_dependencies.sh
    language: script
    types: [python]

in the precommit config . It doesn't work still

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants