Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

python - VS Code error when importing Django module

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

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

Image of my code in error

The error message is the following:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Base(Global) environment No module named Django in my global enviroment

and when I use the same command inside my virtual environment (myDjangoEnv) enter image description here

Fix:

  1. Now what I understood is that pylint uses the python interpreter to analyze our code and mark the error while we write the code.
  2. Since I have three python environments in my system, hence three different python interpreters are available to my VS Code.

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. Click on the bottom left of the screen to change python interpreter. changing python interpreter
  2. Select from the list of available Python interpreters. Make sure you select the appropriate interpreter with respect to the current project. enter image description here

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...