我正在使用PyCharm在一个项目上工作。使用解释器打开并配置该项目,并可以成功运行。远程解释器路径映射正确。这似乎是正确的配置,但PyCharm突出显示了带有“未解决的引用”错误的有效代码,甚至对于内置的Python函数也是如此。为什么即使代码运行,这些似乎也没有被检测到?有没有什么方法可以让PyCharm正确识别这些?
此问题的具体实例是远程解释器,但本地解释器也会出现此问题。
我正在使用PyCharm在一个项目上工作。使用解释器打开并配置该项目,并可以成功运行。远程解释器路径映射正确。这似乎是正确的配置,但PyCharm突出显示了带有“未解决的引用”错误的有效代码,甚至对于内置的Python函数也是如此。为什么即使代码运行,这些似乎也没有被检测到?有没有什么方法可以让PyCharm正确识别这些?
此问题的具体实例是远程解释器,但本地解释器也会出现此问题。
当前回答
我的问题是,Flask-WTF无法通过PyCharm解决。我已经尝试重新安装,然后安装或使缓存无效并重新启动PyCharm,但它仍然不工作。
然后我想到了这个解决方案,它对我来说非常有效。
用Ctrl+Alt+S (Windows)打开项目解释器,然后单击安装(+)一个新包。
输入PyCharm无法解析的包,然后单击“安装包”。单击“确定”。
现在,您将看到库已被解析。
其他回答
有很多解决方案,有些比其他的更方便,但它们并不总是有效。
以下是你可以尝试的,从“快速”到“烦人”:
Do File -> Invalidate Caches / Restart and restart PyCharm. You could also do this after any of the below methods, just to be sure. First, check which interpreter you're running: Run -> Edit Configurations -> Configuration -> Python Interpreter. Refresh the paths of your interpreter: File -> Settings Project: [name] -> Project Interpreter -> 'Project Interpreter': Gear icon -> More... Click the 'Show paths' button (bottom one) Click the 'Refresh' button (bottom one) Remove the interpreter and add it again: File -> Settings Project: [name] -> Project Interpreter -> 'Project Interpreter': Gear icon -> More... Click the 'Remove' button Click the 'Add' button and re-add your interpeter Delete your project preferences Delete your project's .idea folder Close and re-open PyCharm Open your project from scratch Delete your PyCharm user preferences (but back them up first). ~/.PyCharm50 on Mac %homepath%/.PyCharm50 on Windows Switch to another interpreter, then back again to the one you want. Create a new virtual environment, and switch to that environments' interpreter. Create a new virtual environment in a new location -- outside of your project folder -- and switch to that environment's interpreter. Switch to another interpreter altogether; don't switch back.
如果你正在使用Docker,请注意:
确保您使用的是pip3而不是pip,特别是远程docker和docker-compose解释器。 避免影响PYTHONPATH。更多信息请访问:https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000058690-Module-not-found-in-PyCharm-but-externally-in-Python。
如果上面的方法对你不起作用,但你发现了另一个技巧,那么请留下评论。
在所有建议的解决方案都不适合我之后,我终于让它工作了。我正在玩一个django rest框架项目,并使用了我已经设置好的virtualenv。我可以通过将根文件夹标记为源根来修复Pycharm,但是django的服务器会抛出解析异常。所以一个会工作,而另一个不会,反之亦然。
最终,我只需要将子文件夹标记为pycharm中的源代码根。我的结构是这样的
-playground
-env
-playground
第二个playground文件夹是我必须标记为源代码根以便一切正常工作的文件夹。这在我的场景中没有出现任何问题,所以这是一个可行的解决方案。
我只是想分享一下,以防别人用。
使用PyCharm 4.0.6测试(OSX 10.10.3) 以下步骤:
点击PyCharm菜单。 选择项目解释器。 选择齿轮图标。 选择更多按钮。 选择您所在的项目解释器。 “选择目录树”按钮。 选择重载路径列表。
问题解决了!
我必须去文件->无效缓存/重新启动,重新启动Ubuntu 18.04 LTS,然后打开Pycharm和文件->无效缓存/重新启动,然后才清理。
如果你正在使用vagrant,这个错误可能是由错误的python解释器引起的。 在我们的vagrant中,我们使用pyenv,所以我必须将Python解释器的路径路径从/usr/bin/python更改为/home/vagrant/.pyenv/versions/vagrant/bin/python