我正在使用PyCharm在一个项目上工作。使用解释器打开并配置该项目,并可以成功运行。远程解释器路径映射正确。这似乎是正确的配置,但PyCharm突出显示了带有“未解决的引用”错误的有效代码,甚至对于内置的Python函数也是如此。为什么即使代码运行,这些似乎也没有被检测到?有没有什么方法可以让PyCharm正确识别这些?
此问题的具体实例是远程解释器,但本地解释器也会出现此问题。
我正在使用PyCharm在一个项目上工作。使用解释器打开并配置该项目,并可以成功运行。远程解释器路径映射正确。这似乎是正确的配置,但PyCharm突出显示了带有“未解决的引用”错误的有效代码,甚至对于内置的Python函数也是如此。为什么即使代码运行,这些似乎也没有被检测到?有没有什么方法可以让PyCharm正确识别这些?
此问题的具体实例是远程解释器,但本地解释器也会出现此问题。
当前回答
我关闭了所有其他项目,并在Pycharm中单独运行所需的项目。我从pycharm创建了一个单独的virtualenv,并使用pip在其中添加了所有必需的模块。我在项目的解释器中添加了这个虚拟环境。这解决了我的问题。
其他回答
@kelorek适用于我,但之前,在interpereter路径中,我必须添加一些路径。 让说
from geometry_msgs.msg import Twist
下划线为错误,然后在远程机器中运行python:
help("geometry_msgs")
在最后会有路径,我们说:
/opt/ros/foxy/lib/python3.8/site-packages/geometry_msgs/__init__.py
所以你的解释器pycharm路径添加
/opt/ros/foxy/lib/python3.8/site-packages
希望它能帮助你,也帮助我:)
再补充一个:涉及Python解释器选项卡的解决方案都没有帮助,然而,我注意到我必须设置项目依赖项:在有未解决的引用错误的项目中,没有任何依赖项被检查。一旦我检查了,相关的错误就消失了。我不知道为什么一开始就检查了一些,而另一些没有。
如果你正在使用vagrant,这个错误可能是由错误的python解释器引起的。 在我们的vagrant中,我们使用pyenv,所以我必须将Python解释器的路径路径从/usr/bin/python更改为/home/vagrant/.pyenv/versions/vagrant/bin/python
我也有同样的症状在我的例子中,问题的来源是我设置的 自定义属性中的Idea.max.intellisense.filesize =50。 我可以把它设为100。
帮助->编辑自定义属性
有很多解决方案,有些比其他的更方便,但它们并不总是有效。
以下是你可以尝试的,从“快速”到“烦人”:
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。
如果上面的方法对你不起作用,但你发现了另一个技巧,那么请留下评论。