今天在开始跑一个深度学习项目时,在启动时,程序出现了以下的错误,

OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

从表面的意思看就是libiomp5md.dll已经被初始化了导致的错误,这个有可能是因为系统环境中存在多个版本的OpenMP库导致,可能是由于你的电脑使用Anaconda管理了多个Python虚拟环境,并且每个环境中都安装了Pytorch库,导致每个环境中都有libiomp5md.dll

网上大多数人给出的解决方案就是修改系统的环境变量,如下

import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

KMP_DUPLICATE_LIB_OK设置为True即可,但是我测试了有更加简单的办法就是重启电脑,重启电脑之后这个问题就会被修复。