我正在使用PyCharm在一个项目上工作。使用解释器打开并配置该项目,并可以成功运行。远程解释器路径映射正确。这似乎是正确的配置,但PyCharm突出显示了带有“未解决的引用”错误的有效代码,甚至对于内置的Python函数也是如此。为什么即使代码运行,这些似乎也没有被检测到?有没有什么方法可以让PyCharm正确识别这些?
此问题的具体实例是远程解释器,但本地解释器也会出现此问题。
我正在使用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将Python包的init.py文件标记为文本,因此没有将它们包括在分析中,这意味着Python解析无法正确工作。
我的解决方案是:
打开PyCharm设置 导航到编辑器->文件类型 找到Python并将__init__.py添加到Python文件列表中 或查找文本并从文本文件列表中删除__init__.py
文件|无效缓存…重新启动PyCharm会有所帮助。
德米特里的回答对我不起作用。
我打开Project Interpreters,选择“Paths”选项卡,然后点击子菜单中的刷新按钮。它自动填充了一种叫做“蟒蛇骨架”的东西。
编辑:使用PyCharm 3.4.1截图(它隐藏得很好)
以上的方法对我都没用! 如果您正在为项目使用虚拟环境,请确保应用虚拟环境目录中的python.exe文件作为项目的解释器(Alt + Ctrl + Shift + S) 这为我解决了问题。
你在使用virtualenv吗?
如果是这样,你需要通知PyCharm所需的python.exe位置的每一次更改(仅./activate对PyCharm来说是不够的)。
确保Pycharm指向正确的解释器和包: 文件->设置->项目->项目解释器。单击齿轮并在virtualenv的脚本文件夹下选择python.exe