PyCharm 无法正确识别需求 - Python、Django

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29025468/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:00:58  来源:igfitidea点击:

PyCharm Not Properly Recognizing Requirements - Python, Django

pythondjangopycharm

提问by nicorellius

Often requirements show up in requirements.txtlike this:

需求通常是requirements.txt这样显示的:

django-registration

django-registration

But in INSTALLED_APPSit appears as registration. So when you import like:

但在INSTALLED_APPS它显示为registration. 所以当你像这样导入时:

from registration.signals import user_registered

This is valid but PyCharm is showing a red squiggly line under the word registrationin from registration...that says "Package 'registration' is not listed in project requirements".

这是有效的,但PyCharm是出了字下一个红色的波浪线报名from registration...,上面写着“包‘注册’是不是在项目要求上市”。

How can I remedy this?

我该如何补救?

UPDATE

更新

I was able to find a way to make the warning go away but it's not a good solution. If you simply add the package name to the requirements.txtfile the warning goes away, but then that requirement is not valid if you were to install with pip. For example, I'm also using pygooglevoicewhich is how it's written in requirements.txtbut when it's imported in the code, you write:

我能够找到一种方法来消除警告,但这不是一个好的解决方案。如果您只是将包名称添加到requirements.txt文件中,警告就会消失,但是如果您要使用pip. 例如,我也在使用pygooglevoicewhich 是如何编写的,requirements.txt但是当它被导入代码时,你写:

from googlevoice import Voice

This seems weird because I use PyCharm for many projects and I'm just noticing this with a recent project I'm working on...

这看起来很奇怪,因为我在许多项目中都使用了 PyCharm,而我只是在最近正在从事的一个项目中注意到了这一点......

采纳答案by nicorellius

After seeing this phenomenon again, I did some more digging. After setting certain folders as source roots and restarting PyCharm, these reference warnings went away. I think this is a bug in PyCharm.

再次看到这个现象后,我又做了一些挖掘。将某些文件夹设置为源根目录并重新启动 PyCharm 后,这些参考警告消失了。我认为这是 PyCharm 中的一个错误。

回答by MrJ

This seem to be an open issue

这似乎是一个悬而未决的问题

https://youtrack.jetbrains.com/issue/PY-11963

https://youtrack.jetbrains.com/issue/PY-11963

回答by Troy Hoffman

PyCharm should be able to figure this out, but it doesn't. For now, I just add # noinspection PyPackageRequirements to each import with this warning to suppress it. I'd rather not, but it works for now.

PyCharm 应该能够解决这个问题,但它没有。现在,我只是将 # noinspection PyPackageRequirements 添加到每个带有此警告的导入中以抑制它。我宁愿不这样做,但它现在有效。

回答by iver56

JetBrains claims that this is fixed in version 2017.2:

JetBrains 声称这已在 2017.2 版中修复:

https://youtrack.jetbrains.com/issue/PY-11963#comment=27-2248728

https://youtrack.jetbrains.com/issue/PY-11963#comment=27-2248728

I can confirm this (in IntelliJ IDEA). Also, it's worth double-checking that the correct requirements file is specified in Settings -> Tools -> Python Integrated Tools -> Package requirements file

我可以确认这一点(在 IntelliJ IDEA 中)。此外,值得仔细检查是否指定了正确的需求文件Settings -> Tools -> Python Integrated Tools -> Package requirements file