简体   繁体   中英

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.

Below is one of many examples where Pylance is not able to find what I'm looking for. Here, I obviously need models from django.db . But there are only theses 6 suggestions below...

的

Here is what I know or tried:

  • My interpreter is correctly selected (Python 3.10.4)
  • Pylance seems to work perfectly with Python (not Django) related stuff.
  • I'm using Poetry as a package manager, and no virtual env because I work in a self-contained dev container. There is only one python installed on it.
  • In my VS Code config ( using devcontainer.json ): "python.analysis.extraPaths": ["${workspaceFolder}/dj_proj/dj_apps"] -> which works to prevent missing imports. 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
  • I ensured there is a __init__.py pretty much everywhere
  • my sys.path (PYTHONPATH) looks like this (where dj_proj is my django project and dj_apps my apps folder):

['/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.

Any idea on what could be wrong? Thanks!

I believe you should install the Django extension in VS Code. 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:

在此处输入图像描述

I hope this is useful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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