Python 未解决的参考:PyCharm 中的“django”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42145656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unresolved reference: 'django' error in PyCharm
提问by m4jesticsun
I'm trying to import:
我正在尝试导入:
from django.db import models
PyCharm underlines django.db
and complains: Unresolved reference 'django'
.
PyCharm 强调django.db
并抱怨:Unresolved reference 'django'
.
How do I get PyCharm to recognize Django?
如何让 PyCharm 识别 Django?
回答by Elisabeth Shevtsova
I assume you're using a virtualenv which is located in the same directory as other project files. Python should know exactly that path. So, it's possibly that Pycharm is using the wrong Interpreter.
我假设您使用的是与其他项目文件位于同一目录中的 virtualenv。Python 应该确切地知道那条路径。因此,Pycharm 可能使用了错误的解释器。
Go to Settings -> Project: -> Project Interpreter -> switch to the right path
转到设置-> 项目:-> 项目解释器-> 切换到正确的路径
In the interpreter packages, there should be Django package installed. If not, do it here/in terminal.
在解释器包中,应该安装了 Django 包。如果没有,请在此处/在终端中执行。
回答by Ayush Vatsyayan
Above answers are answered partially.
以上答案部分回答。
Problem Description: I've multiple versions of python installed and Django is installed in one of Python version. There are two issues
问题描述:我安装了多个版本的 python,并且 Django 安装在其中一个 Python 版本中。有两个问题
Issue 1:Pycharm has wrong python interpreter. For this the
Project Interpreter
has to be changed to Python version where Django is installed. Solution: Follow all steps.Issue 2:Django isn't listed as package. For this we need make available the installed python packages in the Pycharm environment. Solution: Follow all steps except step 3.
问题 1:Pycharm 有错误的 Python 解释器。为此,
Project Interpreter
必须将其更改为安装了 Django 的 Python 版本。解决方案:按照所有步骤操作。问题 2:Django 未作为包列出。为此,我们需要在 Pycharm 环境中提供已安装的 python 包。解决方案:执行除步骤 3 以外的所有步骤。
Solution:Peform following steps.
解决方法:执行以下步骤。
- In preference/settings go to
Project
>Project Interpreter
- On right hand side click on
settings icon
>Add Local
- Select correct Python version from
Base Interpreter
- Mark the checkbox against
Inherit global site-packages
andMake available to all projects
- Click
ok
- 在偏好/设置中转到
Project
>Project Interpreter
- 在右侧点击
settings icon
>Add Local
- 从中选择正确的 Python 版本
Base Interpreter
- 标记复选框对
Inherit global site-packages
和Make available to all projects
- 点击
ok
Once this is done in Project Intepreter
you will be able to see Django
in the Package list.
完成此操作后,Project Intepreter
您将能够Django
在包列表中看到。
回答by Igor Z
Mark root folder of project as 'Sources root', it works for me. Content Root
将项目的根文件夹标记为“源根”,它对我有用。 内容根
回答by GrahamJ
I got this problem and it stopped my unit tests from running.
我遇到了这个问题,它阻止了我的单元测试运行。
I found that PyCharm, during an upgrade, had set my Python Interpreter
to one which was in a virtual environment, which I do not use any more for this project. So I set it to my global Python environment in C:\Python
and everything works like a charm.
我发现 PyCharm 在升级过程中将 my 设置Python Interpreter
为虚拟环境中的一个,我不再在这个项目中使用它。所以我将它设置到我的全局 Python 环境中C:\Python
,一切都像魅力一样。
I hope this will help someone.
我希望这会帮助某人。
回答by aircraft
If you create the project use python2.7, and in python2.7 path you installed the django, the project works normal. Then you switch the Project Interpreter to the python3.5, but this path did not install the django, then you will get this issue.
如果使用python2.7创建项目,并且在python2.7路径下安装了django,则项目正常运行。然后你把Project Interpreter切换到python3.5,但是这个路径没有安装django,那么就会出现这个问题。
回答by Milad
if you have installed Django successfully before, in a different location from project location:
in pycharm go to the setting
>Project
>project interpreter
.
right side of the Project Interpreter, click on setting icon and choose add local
.
如果您之前已经成功安装了 Django,请在与项目位置不同的位置:在 pycharm 中转到setting
> Project
> project interpreter
。在 Project Interpreter 的右侧,单击设置图标并选择add local
。
then in New Environment check these two checkbox:
然后在新环境中选中这两个复选框:
- inherit global site-packages
- make available to all projects
- 继承全局站点包
- 提供给所有项目
then press Ok
and Apply
button and wait installing the interpreter to your project location.
然后按Ok
和Apply
按钮并等待将解释器安装到您的项目位置。
回答by Windo
You can make pyCharm recognize it by setting it to use your virtualenv setup that I assume you had it already for the project
您可以通过将其设置为使用您的 virtualenv 设置来使 pyCharm 识别它,我假设您已经为该项目设置了它
STEP 1: open preferences PyCharm > Preferences
第 1 步:打开首选项PyCharm > Preferences
STEP 2:Search for interpreter
and on the right window, select your virtual environment to be used in Project Interpreter
STEP 2:搜索interpreter
并在右侧窗口中,选择您要使用的虚拟环境Project Interpreter
STEP 3 (ADDITIONAL):adding your environment to the list if it didn't show up
第 3 步(附加):如果您的环境没有出现,请将其添加到列表中
select show all interpreter, and then on the pop up window, click + icon on the bottom left
select radio button
Existing Environment
and select your virtual env, and then check "Make available for all project" if you use the env for other project, and click ok
STEP 4:On the preferences window, click apply or straight click the ok button. wait couple second and django variable should be available in your project
第 4 步:在首选项窗口中,单击应用或直接单击确定按钮。等待几秒钟,django 变量应该在您的项目中可用
回答by M_Gh
I had this problem too. In fact, I faced with this problem because django
package had not been installed in Pycharm. Therefore, I went to this path and clicked on plus sign;then, I searched django
and installed it. It worked well.
我也有这个问题。事实上,我遇到这个问题是因为django
Pycharm 中没有安装包。因此,我去了这个路径并点击了加号;然后,我搜索django
并安装了它。它运作良好。
file
>setting
>project
>project interpreter
file
> setting
> project
>project interpreter
回答by MarsYoung
回答by user2228903
I used virtualenv in my project and the following steps is works for me.
我在我的项目中使用了 virtualenv,以下步骤对我有用。
Settings -> Project:XXX -> Project Interpreter --> click the right side icon next to project interpreter -> more -> select the virtualenv interpreter
Settings -> Project:XXX -> Project Interpreter --> 点击project interpreter旁边的右侧图标-> more -> 选择virtualenv interpreter
hope it can help someone
希望它可以帮助某人