报错现象:
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/.
我的运行软件为pycharm,用的解释器为anaconda下创建的pytorcn虚拟环境。
解决方案
第一步:
究其原因其实是,anaconda的环境下存在两个libiomp5md.dll文件。所以直接去虚拟环境的路径下搜索这个文件,可以看到在环境里有两个dll文件:
其中第一个是torch路径下的,第二个是虚拟环境本身路径下的,转到第二个目录下把它剪切到其他路径下备份就好(最好把路径也备份一下)。
第一个的路径为:
D:\Users\Hasee\anaconda3\envs\pytorch\Lib\site-packages\torch\lib
第二个的路径为:
D:\Users\Hasee\anaconda3\envs\pytorch\Library\bin
我经过第一步操作,问题任然没有解决。于是在此次基础上,进行了下面的操作,这步操作下来,问题就解决了。
第二步:
在运行的程序代码中,加入下面代码:
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
我经过上面的两步操作后,成功解决了问题。
总结:
上面两步,可以是独立的两种解决方案,也可以是互相铺垫的一种解决方案,总之,每个人的问题情况或多或少有点不一样,所以对于各种方法借鉴参考,结合自己的实际情况分析研究,就会找到解决自己问题的方案。