我正在使用PyCharm在一个项目上工作。使用解释器打开并配置该项目,并可以成功运行。远程解释器路径映射正确。这似乎是正确的配置,但PyCharm突出显示了带有“未解决的引用”错误的有效代码,甚至对于内置的Python函数也是如此。为什么即使代码运行,这些似乎也没有被检测到?有没有什么方法可以让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。

如果上面的方法对你不起作用,但你发现了另一个技巧,那么请留下评论。

其他回答

文件|无效缓存…重新启动PyCharm会有所帮助。

我的问题是,Flask-WTF无法通过PyCharm解决。我已经尝试重新安装,然后安装或使缓存无效并重新启动PyCharm,但它仍然不工作。

然后我想到了这个解决方案,它对我来说非常有效。

用Ctrl+Alt+S (Windows)打开项目解释器,然后单击安装(+)一个新包。

输入PyCharm无法解析的包,然后单击“安装包”。单击“确定”。

现在,您将看到库已被解析。

在Windows 10 10.0上的PyCharm 2020.1.4(社区版)中。在PyCharm的Settings下:File > Settings > Project Structure 我对Project Structure做了两个改动: 主文件夹标记为源和 odoo文件夹与我排除的所有应用程序 截图显示了我所做的。 之后,我重新启动PyCharm: File > Invalidate Caches / Restart…

未解决的引用错误已被删除

在我的情况下,检查错误显示由于一个非常具体的python代码的情况。 包含两个numpy函数和两个列表访问的min函数会使我的代码检查给出这种错误。

在下面的例子中删除'd=0'行会像预期的那样给出一个无法解决的引用错误,但是读取代码检查器并不能消除这个错误。之后我仍然可以毫无问题地执行代码。

import numpy as np
def strange(S, T, U, V):
    d = 0
    print min(np.abs(S[d]), np.abs(T[d]), U[d], V[d])

清除缓存和重新加载路径列表不工作。只有使用以下示例补丁之一更改代码才能工作:

“min”参数的另一种顺序:简图上的S U T V,但不是S T U V或T S U V 使用方法而不是函数:S[d].abs()而不是np.abs(S[d]) 使用内置的abs()函数 将一个数字添加到所选参数:U[d] + 0。

其他答案所建议的无效缓存对我不起作用。在我的案例中,我发现的问题是PyCharm将Python包的init.py文件标记为文本,因此没有将它们包括在分析中,这意味着Python解析无法正确工作。

我的解决方案是:

打开PyCharm设置 导航到编辑器->文件类型 找到Python并将__init__.py添加到Python文件列表中 或查找文本并从文本文件列表中删除__init__.py