简体   繁体   English

Pylance Intellisense 无法按预期使用 Django

[英]Pylance Intellisense not working as expected with Django

I'm trying to understand how to configure Pylance to make it work correctly in my Django project.我试图了解如何配置 Pylance 以使其在我的 Django 项目中正常工作。

Below is one of many examples where Pylance is not able to find what I'm looking for.以下是 Pylance 无法找到我要查找的内容的众多示例之一。 Here, I obviously need models from django.db .在这里,我显然需要来自django.dbmodels But there are only theses 6 suggestions below...但是下面只有这6条建议......

的

Here is what I know or tried:这是我所知道或尝试过的:

  • My interpreter is correctly selected (Python 3.10.4)我的解释器已正确选择 (Python 3.10.4)
  • Pylance seems to work perfectly with Python (not Django) related stuff. Pylance 似乎与 Python(不是 Django)相关的东西完美配合。
  • I'm using Poetry as a package manager, and no virtual env because I work in a self-contained dev container.我使用 Poetry 作为 package 经理,没有虚拟环境,因为我在一个独立的开发容器中工作。 There is only one python installed on it.上面只安装了一个python。
  • In my VS Code config ( using devcontainer.json ): "python.analysis.extraPaths": ["${workspaceFolder}/dj_proj/dj_apps"] -> which works to prevent missing imports.在我的 VS 代码配置中(使用 devcontainer.json ): "python.analysis.extraPaths": ["${workspaceFolder}/dj_proj/dj_apps"] -> 用于防止丢失导入。 I have no false warnings about missing imports, just the inability to see the right suggestions in intellisense.我没有关于丢失导入的错误警告,只是无法在智能感知中看到正确的建议。
  • I cleaned __pycache__ and .pyc files -> no effect我清理__pycache__.pyc文件 -> 没有效果
  • I ensured there is a __init__.py pretty much everywhere我确保到处都有一个__init__.py
  • my sys.path (PYTHONPATH) looks like this (where dj_proj is my django project and dj_apps my apps folder):我的sys.path (PYTHONPATH) 看起来像这样(其中dj_proj是我的 django 项目和dj_apps我的应用程序文件夹):

['/workspace/dj_proj/dj_apps',
 '/workspace/dj_proj',
 '/usr/local/lib/python310.zip',
 '/usr/local/lib/python3.10',
 '/usr/local/lib/python3.10/lib-dynload',
 '',
 '/usr/local/lib/python3.10/site-packages']

I'm suspecting this PYTHONPATH variable to be messy or not sorted the right way, but I'm not sure.我怀疑这个 PYTHONPATH 变量是混乱的或者没有以正确的方式排序,但我不确定。

Any idea on what could be wrong?关于可能出什么问题的任何想法? Thanks!谢谢!

I believe you should install the Django extension in VS Code.我相信您应该在 VS Code 中安装 Django 扩展。 Even though some obvious stuff gets always overseen by the language support, at least that extension contains some useful stuff like pre-made models, CBVs and widely-used imports such as these:尽管语言支持总是会监督一些明显的东西,但至少该扩展包含一些有用的东西,如预制模型、CBV 和广泛使用的导入,例如:

在此处输入图像描述

I hope this is useful to you.我希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM