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

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

问题描述

我正在使用Web应用程序,并且我使用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代码可以使用三个不同的python解释器.

因此,如果VS代码在基本环境中使用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解释器列表中选择.确保针对当前项目选择适当的解释器.
  1. Click on the bottom left of the screen to change python interpreter.
  2. Select from the list of available Python interpreters. Make sure you select the appropriate interpreter with respect to the current project.

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

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