PyCharm显示有效代码的未解析引用错误

问题:PyCharm显示有效代码的未解析引用错误

我正在使用PyCharm进行项目。该项目将打开并使用解释器进行配置,并且可以成功运行。远程解释器路径已正确映射。这似乎是正确的配置,但是PyCharm突出显示了我的有效代码,并带有“未解决的引用”错误,即使对于内置Python函数也是如此。为什么即使代码运行也似乎没有检测到这些?有没有办法让PyCharm正确识别这些?


该问题的特定实例与远程解释器有关,但该问题也出现在本地解释器上。

I am using PyCharm to work on a project. The project is opened and configured with an interpreter, and can run successfully. The remote interpreter paths are mapped properly. This seems to be the correct configuration, but PyCharm is highlighting my valid code with “unresolved reference” errors, even for built-in Python functions. Why don’t these seem to be detected, even though the code runs? Is there any way to get PyCharm to recognize these correctly?


This specific instance of the problem is with a remote interpreter, but the problem appears on local interpreters as well.


回答 0

档案| 无效的缓存…并重新启动PyCharm会有所帮助。

File | Invalidate Caches… and restarting PyCharm helps.


回答 1

德米特里的回应对我没有用。

我可以通过以下方法来工作:转到项目解释器,选择“路径”选项卡,然后单击该子菜单中的刷新按钮。它会自动填充“ python-skeletons”。

编辑:使用PyCharm 3.4.1的屏幕截图(隐藏得很好)

Dmitry’s response didn’t work for me.

I got mine working by going to Project Interpreters, Selecting the “Paths” tab, and hitting the refresh button in that submenu. It auto-populated with something called “python-skeletons”.

edit: screenshot using PyCharm 3.4.1 (it’s quite well hidden)


回答 2

有许多解决方案,其中一些比其他解决方案更方便,而且它们并不总是有效。

这是您可以尝试的所有方法,从“快速”变为“烦人”:

  • 执行File-> Invalidate Caches / Restart然后重新启动PyCharm。
    • 可以肯定的是,您也可以使用以下任何一种方法进行此操作。
  • 首先,检查您正在运行的解释器:Run-> Edit Configurations-> Configuration-> Python Interpreter
  • 刷新解释器的路径:
    • File -> Settings
    • Project: [name]-> Project Interpreter->“项目解释器”:齿轮图标->More...
    • 点击“显示路径”按钮(下一个)
    • 点击“刷新”按钮(下一个)
  • 删除解释器并再次添加:
    • File -> Settings
    • Project: [name]-> Project Interpreter->“项目解释器”:齿轮图标->More...
    • 点击“删除”按钮
    • 点击“添加”按钮,然后重新添加您的信息
  • 删除项目首选项
    • 删除项目的.idea文件夹
    • 关闭并重新打开PyCharm
    • 从头开始打开项目
  • 删除您的PyCharm用户首选项(但先备份它们)。
    • ~/.PyCharm50 在Mac上
    • %homepath%/.PyCharm50 在Windows上
  • 切换到另一个解释器,然后再次返回到您想要的解释器。
  • 创建一个新的虚拟环境,然后切换到该环境的解释器。
  • 完全切换到另一个口译员,不要切换回去。

There are many solutions to this, some more convenient than others, and they don’t always work.

Here’s all you can try, going from ‘quick’ to ‘annoying’:

  • 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.
  • Switch to another interpreter altogether, don’t switch back.

回答 3

就我而言,这是目录结构。我的项目如下所示:

+---dir_A
    +---dir_B
        +app
        |
        \-run.py

因此,右键单击dir_b>“将目录标记为”>“项目根目录”

In my case it was the directories structure. My project looks like this:

+---dir_A
    +---dir_B
        +app
        |
        \-run.py

So right click on dir_b > “mark directory as” > “project root”


回答 4

当无效缓存或刷新路径不起作用时,我发现自己删除并重新添加了远程解释器以解决此问题。

如果将新虚拟机添加到多虚拟机设置中,我会不时地使用vagrant,并且转发的端口会发生更改,这在PyCharm尝试为SSH使用错误的端口时会感到困惑。更改端口似乎无济于事。

I find myself removing and re-adding the remote interpreter to fix this problem when Invalidating Caches or Refreshing Paths does not work.

I use vagrant and every once and awhile if I add a new VM to my multi-vm setup, the forwarded port changes and this seems to confuse PyCharm when it tries to use the wrong port for SSH. Changing the port doesn’t seem to help the broken references.


回答 5

如果没有其他解决方案适合您,请尝试(备份)并删除〜/ .PyCharm40文件夹,然后重新打开PyCharm。这也会杀死您的所有偏好。

在Mac上,您要删除〜/ Library / Caches / Pycharm40和〜/ Library / Preferences / PyCharm40。

在Windows上:C:\ Users \ $ USER.PyCharm40。

If none of the other solutions work for you, try (backing up) and deleting your ~/.PyCharm40 folder, then reopening PyCharm. This will kill all your preferences as well.

On Mac you want to delete ~/Library/Caches/Pycharm40 and ~/Library/Preferences/PyCharm40.

And on Windows: C:\Users\$USER.PyCharm40.


回答 6

按照以下步骤在PyCharm 4.0.6(OSX 10.10.3)中进行了测试:

  1. 点击PyCharm菜单。
  2. 选择项目解释器。
  3. 选择齿轮图标。
  4. 选择更多按钮。
  5. 选择您所在的项目口译员。
  6. 选择目录树按钮。
  7. 选择重新加载路径列表。

问题解决了!

Tested with PyCharm 4.0.6 (OSX 10.10.3) following this steps:

  1. Click PyCharm menu.
  2. Select Project Interpreter.
  3. Select Gear icon.
  4. Select More button.
  5. Select Project Interpreter you are in.
  6. Select Directory Tree button.
  7. Select Reload list of paths.

Problem solved!


回答 7

好简单

您只需将根目录标记为:SOURCE ROOT(红色),并将您的应用程序标记为EXCLUDED ROOT(蓝色),

那么未解决的参考将消失。

  • 如果您使用PyChram pro,它将自动为您执行此操作。

very easy

you just have to mark your root directory as: SOURCE ROOT (red), and your applications: EXCLUDED ROOT (blue),

then the unresolved reference will disappear.

  • if you use PyChram pro it do this for you automaticlly.

回答 8

很抱歉碰到这个问题,但是我有一个重要的更新。

如果您使用的是其他版本,则可能还需要将项目解释器恢复为Python 2.7.6。在没有其他建议解决了我的问题之后,这对我的Ubuntu安装PyCharm 4.04 Professional很有用。

Sorry to bump this question, however I have an important update to make.

You may also want to revert your project interpreter to to Python 2.7.6 if you’re using any other version than that This worked for me on my Ubuntu installation of PyCharm 4.04 professional after none of the other recommendations solved my problem.


回答 9

您可以尝试关闭Pycharm,.idea从项目中删除文件夹,然后再次启动Pycharm并重新创建项目。这对我有用,而使缓存无效却没有。

You might try closing Pycharm, deleting the .idea folder from your project, then starting Pycharm again and recreating the project. This worked for me whereas invalidating cache did not.


回答 10

在没有任何建议的解决方案对我起作用之后,我终于使这项工作奏效了。我正在玩django rest框架项目,并且正在使用我已为其设置的virtualenv。我可以通过将根文件夹标记为源根来修复Pycharm,但是django的服务器会抛出解决异常。因此,一个将在另一个不起作用时起作用,反之亦然。

最终,我只需要将子文件夹标记为pycharm中的源即可。所以我的结构是这样的

-playground
     -env
     -playground

第二个Playground文件夹是我必须将其标记为源根目录的文件夹,以便一切正常工作。对于我的情况,这没有任何问题,因此这是一个可行的解决方案。

只是以为我愿意与他人分享,以防其他人可以使用它。

I finally got this working after none of the proposed solutions worked for me. I was playing with a django rest framework project and was using a virtualenv I had setup with it. I was able to get Pycharm fixed by marking the root folder as the sources root, but then django’s server would throw resolve exceptions. So one would work when the other wouldn’t and vice versa.

Ultimately I just had to mark the subfolder as the sources root in pycharm. So my structure was like this

-playground
     -env
     -playground

That second playground folder is the one I had to mark as the sources root for everything to work as expected. That didn’t present any issues for my scenario so it was a workable solution.

Just thought I’d share in case someone else can use it.


回答 11

这也可能是python版本问题。我必须选择合适的设备才能使其正常工作。

It could also be a python version issue. I had to pick the right one to make it work.


回答 12

如果您只想忽略一些“未解决的引用”错误,也可以通过将其放在类/方法/函数的前面来明确地告诉它PyCharm:

# noinspection PyUnresolvedReferences

If you want to ignore only some “unresolved reference” errors, you can also tell it PyCharm explicitly by placing this in front of your class/method/function:

# noinspection PyUnresolvedReferences

回答 13

我关闭了所有其他项目,并在Pycharm中独立运行所需的项目。我从pycharm创建了一个单独的virtualenv,并使用pip在其中添加了所有必需的模块。我在项目的解释器中添加了此虚拟环境。这解决了我的问题。

I closed all the other projects and run my required project in isolation in Pycharm. I created a separate virtualenv from pycharm and added all the required modules in it by using pip. I added this virtual environment in project’s interpreter. This solved my problem.


回答 14

Geeze真是一场噩梦,我将不同的StackOVerflow合并在一起回答:

  1. 切换到本地解释器/usr/bin/pythonX.X并应用
  2. 查看以上答案的路径
  3. 查找骨骼路径。我的是(/home/tim/Desktop/pycharm-community-2016.2.3/helpers/python-skeletons)
  4. 切换回virt解释器并手动添加骨骼路径(如果该路径未自动显示)。

Geeze what a nightmare, my amalgamation of different StackOVerflow answers:

  1. Switch to local interpreter /usr/bin/pythonX.X and apply
  2. View paths like above answer
  3. Find skeletons path. Mine was (/home/tim/Desktop/pycharm-community-2016.2.3/helpers/python-skeletons)
  4. Switch back to virt interpreter and add the skeletons path manually if it didn’t automatically show up.

回答 15

以上解决方案都不适合我!
如果您在项目中使用虚拟环境,请确保将python.exe虚拟环境目录中的文件用作项目的解释器,(Alt + Ctrl + Shift + S) 这为我解决了这个问题。

None of the above solutions worked for me!
If you are using virtual environment for your project make sure to apply the python.exe file that is inside your virtual environment directory as interpreter for the project (Alt + Ctrl + Shift + S) this solved the issue for me.


回答 16

在我的情况下,由于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”参数的另一排序:示意性显示SUTV,但不显示STUV或TSUV
  • 使用方法代替函数:S [d] .abs()代替np.abs(S [d])
  • 使用内置的abs()函数
  • 在选择的参数上加上数字:U [d] + 0。

In my case the inspection error shows up due to a very specific case of python code. A min function that contains two numpy functions and two list accesses makes my code inspection give this kind of errors.

Removing the ‘d=0’ line in the following example gives an unresolved reference error as expected, but readding doesn’t make the error go away for the code inspector. I can still execute the code without problems afterwards.

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])

Clearing caches and reloading list of paths doesn’t work. Only altering the code with one of the following example patches does work:

  • Another ordering of the ‘min’ parameters: schematically S U T V but not S T U V or T S U V
  • Using a method instead of the function: S[d].abs() instead of np.abs(S[d])
  • Using the built-in abs() function
  • Adding a number to a parameter of choice: U[d] + 0.

回答 17

没有答案能解决我的问题。

对我来说,这是切换环境并回到相同的环境。 File->Settings->Project interpreter

我正在使用conda环境。

None of the answers solved my problem.

What did it for me was switching environments and going back to the same environment. File->Settings->Project interpreter

I am using conda environments.


回答 18

为我工作了很多简单的动作


File > Settings > Project > Project Interpreter 
> Select "No interpreter" in the "Project interpreter" list 
> Apply > Set your python interpreter again > Click Apply

利润-Pycharm正在更新骨骼,一切都很好。

Worked for me with much simplier action:


File > Settings > Project > Project Interpreter 
> Select "No interpreter" in the "Project interpreter" list 
> Apply > Set your python interpreter again > Click Apply

Profit – Pycharm is updating skeletons and everything is fine.


回答 19

如果使用vagrant错误,则可能是错误的python解释器引起的。在我们vagrant使用的过程中pyenv,我不得不将Python Interpreter path路径从更改/usr/bin/python/home/vagrant/.pyenv/versions/vagrant/bin/python

If you are using vagrant the error can be caused by wrong python interpreter. In our vagrant we are using pyenv so I had to change Python Interpreter path path from /usr/bin/python to /home/vagrant/.pyenv/versions/vagrant/bin/python


回答 20

我有一个项目,其中一个文件src/导入同一目录中的另一个文件。为了让PyCharm能够识别我必须去File> Settings> Project> Project Structure> selectsrc文件夹,然后单击“标记为:源”

来自https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html

源根目录包含实际的源文件和资源。PyCharm使用源根作为解决导入的起点

I have a project where one file in src/ imports another file in the same directory. To get PyCharm to recognize I had to to go to File > Settings > Project > Project Structure > select src folder and click “Mark as: Sources”

From https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html

Source roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports


回答 21

我的问题是Flask-WTF无法由PyCharm解决。我试图重新安装,然后安装或使缓存无效并重新启动 PyCharm,但仍然无法正常工作。

然后我想出了这个解决方案,它对我来说非常合适。

  1. 通过Ctrl + Alt + S(Windows)打开项目解释器,然后单击“ 安装”(+)新包装。

  1. 键入PyCharm无法解析的软件包,然后单击“ 安装软件包”。然后单击“ 确定”

现在,您将看到库已解决。

My problem is that Flask-WTF is not resolved by PyCharm. I have tried to re-install and then install or Invalidate Cache and Restart PyCharm, but it’s still not working.

Then I came up with this solution and it works perfectly for me.

  1. Open Project Interpreter by Ctrl+Alt+S (Windows) and then click Install (+) a new packgage.

  1. Type the package which is not resolved by PyCharm and then click Install Package. Then click OK.

Now, you’ll see your library has been resolved.


回答 22

您在使用virtualenv吗?

如果是这样,则您需要在所需python.exe的位置上发生的每次更改都通知PyCharm(仅./activate不足以实现PyCharm)

确保Pycharm指向正确的干预者和程序包:文件->设置->项目->项目解释器。单击齿轮,然后在virtualenv的Scripts文件夹下选择python.exe

Are you using virtualenv?

if so, you need to notify PyCharm for every change in the location of the the desired python.exe (merely ./activate is not enough for PyCharm)

Make sure Pycharm points to the correct interpetor and packages: File -> Settings -> Project -> Project Interpreter. Click the gear and choose python.exe under virtualenv’s Scripts folder