【问题标题】:How to set VSCode settings, so that an import is possible?如何设置 VSCode 设置,以便可以导入?
【发布时间】:2021-10-23 17:54:20
【问题描述】:

我已经克隆了一个 github repo,它的项目结构如下(在 Lib\site-packages, Scripts\ 和 static\ 中有一些文件和子文件夹):

-e_commerce\
   --.idea\
   --e_commerce_website\
      ----.gitignore.txt
      ----db.slite3
      ----manage.py
      ----requirements.txt
      ---.idea\
      ---.vs\
         ----e_commerce\v16\.suo
         ----ProjectSettings.json
         ----slnx.sqlite
         ----VSWorkspaceState.json
      ---e_commerce_env\
         ----Include\
         ----Lib\site-packages\
         ----Scripts\
         ----pyvenv.cfg
      ---ecom\
         ----__pycache__\
         ----___init___.py
         ----asgi.py
         ----settings.py
         ----urls.py
         ----wsgi.py
      ---static\
      ---store\
         ----__pycache__\
         ----migrations\
         ----templates\
         ----___init__.py
         ----admin.py
         ----apps.py
         ----models.py
         ----tests.py
         ----urls.py
         ----views.py

例如,在 apps.py(和其他文件)中会发生错误: 无法从 sourcePylance 解析导入“django.apps”。 ...因为这条线而显示:

from django.apps import AppConfig

我创建了一个环境 e_commerce_env,当我从 cmd / powershell 运行服务器时,它可以成功运行。我在环境中检查了 django 的安装位置,结果发现路径等于: c:\users\user\desktop\e_commerce\e_commerce_website\e_commerce_env\lib\site-packages
我应该在 VSCode 中的某处设置此路径以使其正常工作吗?

【问题讨论】:

    标签: python django visual-studio-code import


    【解决方案1】:

    产生此错误是因为它没有检测到已安装的模块,这是 Pylance(VS 代码编辑器)错误,而不是您的代码中的错误。

    您必须为 Pylance 选择正确的 Python 环境才能检测已安装的模块。寿,it should switch to the correct env by default,但你可以尝试禁用和启用扩展以防它被窃听,如果没有,try selecting manually the desired environment

    【讨论】:

      【解决方案2】:

      我相信你需要在 VSCode 中设置 Python 解释器。请参阅此链接:

      https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment

      这实际上会告诉 VSCode 使用您在命令行中使用的相同解释器。

      尽管您使用的命令提示符位于 VSCode 中,但您基本上有两个地方需要激活/选择环境。

      【讨论】:

        【解决方案3】:

        点击这里选择解释器:

        您可以参考official docs了解更多详情。

        【讨论】:

          猜你喜欢
          • 2021-02-25
          • 2013-05-15
          • 2014-05-02
          • 1970-01-01
          • 2010-10-16
          • 1970-01-01
          • 2011-08-16
          • 2018-10-23
          • 2012-01-08
          相关资源
          最近更新 更多