使用pylint时无法导入'celery'但Django运行正常的问题求助
使用pylint时无法导入'celery'但Django运行正常的问题求助
我正在做一个Django项目,用uv作为包管理器,依赖都通过pyproject.toml管理,以下是我的配置相关部分:
pyproject.toml(相关片段)
[project] name = "port-backend" version = "0.1.0" description = "Backend service for port.az" readme = "README.md" requires-python = ">=3.12" dependencies = [ "celery>=5.4.0", "django==4.2", "djangorestframework>=3.15.2", "djangorestframework-simplejwt>=5.5.0", "drf-spectacular>=0.28.0", ] [dependency-groups] dev = [ "pylint>=2.0.0", ] [tool.pylint.MASTER] ignore-paths = ['.venv/'] disable = [ 'C0415', # Import outside toplevel 'E0401', # Import error ]
已执行的步骤
- 我运行了
uv sync,创建了新的.venv虚拟环境并安装了所有依赖。 - VS Code检测到了这个新环境,我选择使用它,并且确认虚拟环境已经激活。
- 运行
uv run manage.py runserver完全正常,说明Django和Celery都安装正确。 - 但是运行
uv run pylint .时出现了以下错误:
************* Module manage manage.py:12:8: C0415: Import outside toplevel (django.core.management.execute_from_command_line) (import-outside-toplevel) ************* Module config.celery_app config/celery_app.py:3:0: E0401: Unable to import 'celery' (import-error)
我已经尝试过的方法
- 用
uv pip list确认了Celery已经安装在.venv里,列表里确实有celery。 - 确认VS Code的终端里
.venv已经激活(终端显示(port-backend) ➜ port_backend))。 - 尝试运行
uv run python -c "import celery",这个命令完全没有报错。
我的问题
为什么pylint找不到Celery(报E0401: Unable to import 'celery'错误),但Django和Celery在manage.py里运行完全正常?我该怎么让pylint正确识别已安装的依赖?
备注:内容来源于stack exchange,提问作者Zaman Kazimov
相关产品推荐
相关产品推荐

