导入 Django 模块时出现 VS Code 错误 [英] VS Code error when importing Django module

查看:35
本文介绍了导入 Django 模块时出现 VS Code 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网络应用程序,我使用 Django 作为框架.
我在 macOS 上使用 VS Code.

I'm working on a web app and I use Django as framework.
I'm using VS Code on a macOS.

当我尝试导入一些 Django 模块时出现错误.这是我错误代码的截图.

I get an error when I try to import some Django module. This is a screenshot of my code in error.

错误信息如下:

[pylint] E0401:无法导入 'django.conf.urls'

[pylint] E0401:Unable to import 'django.conf.urls'

推荐答案

我在使用 Python 虚拟环境时也遇到了这个错误.就我而言,这是因为我在我的虚拟环境中安装了 Django,而我的基础环境不包含任何名为 Django 的模块.

I too was facing this error while working with Python virtual environments. In my case, it was happening because I have installed Django on my virtual environment and my base environment didn't contain any module named Django.

基础(全球)环境

当我在虚拟环境中使用相同的命令时 (myDjangoEnv)

and when I use the same command inside my virtual environment (myDjangoEnv)

修正:

  1. 现在我的理解是pylint使用python解释器来分析我们的代码并在我们编写代码时标记错误.
  2. 由于我的系统中有三个 Python 环境,因此我的 VS Code 可以使用三个不同的 Python 解释器.

所以如果 VS code 使用来自基础环境的 Python 解释器,它将无法识别 Django 模块(导入错误).因此,您需要将 Python 解释器更改为虚拟环境中存在的解释器.

So if VS code uses the Python interpreter from the base environment, it will not recognize the Django module (import Error). Therefore, you are required to change the Python interpreter to the one present in your virtual environment.

听起来很复杂,其实很简单:

It sounds complicated but it is pretty simple:

  1. 单击屏幕左下角以更改 python 解释器.
  2. 从可用的 Python 解释器列表中选择.确保根据当前项目选择合适的解释器.

这篇关于导入 Django 模块时出现 VS Code 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆