受疫情影响,暂不能返校,但是科研任务还是不能荒废的,今天一鼓作气,准备在自己的笔记本上运行“落满灰尘”的代码,点击运行按钮,报错“ModuleNotFoundError: No module named 'tensorflow'”,……对了!是自己上次新装了anaconda后没有安装必要的库。

于是在终端里输入“conda install tensorflow”,因为网速太慢的问题,一直出现中断,多次尝试后,决定使用清华大学镜像,按照一篇教程里的输入三条指令,添加镜像路径:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

再次进行 conda install tensorflow  ,这次总算是成功了。

赶紧运行“灰尘满满”的代码,结果这次又报错了“AttributeError: module 'tensorflow' has no attribute 'placeholder'”,原来这次安装tensorflow安装的是最新版本,2.0以上的版本里去掉了placeholder的功能,现在要么是修改自己的python代码,要么是重新安装低版本的tensorflow,经过种种考虑我选择了后者。

首先我建立了一个新的环境 conda create -n tensorflow_v1 ,并激活环境:conda activate tensorflow_v1

在新的环境里安装我的低版本库,多方查找后,我选择了以下命令:

conda search tensorflow
conda install tensorflow=1.15.0

又是多次的retry(由于网络问题,下载过程经常中断,需要重新下载),总算是安装完所需的package了。接下来就需要在新的环境中安装spyder,可以在anaconda的界面中直接选择环境,点击install spyder即可,也可以在命令行中进行。

Spyder装好后,运行我自己的代码,报错(跪了):

kernel died,restarting

我又运行了一个简单的测试代码,可以正常显示结果,说明应该是代码的问题,也可能是spyder一些配置和我的代码有冲突。

于是各种查找资料,安装一些不知道是啥的东西,重启终端,anaconda,spyder……能试的全试了,现在的情况是不报错了,但是程序就悄无声息地停止运行了,没有任何提示,也没有运行完成(还不如报错呢!)

在经过了几天的搁置后,我突然想到,不一定非要使用spyder,可以直接在终端使用“python main.py”命令运行我的文件,这次终于又了报错!

OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib 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/.

Abort trap: 6

查找资料后发现可能是由于安装了多个库,于是在运行程序时链接到了多个函数,导致出错,解决方法是将环境变量设为静态链接,只让链接器链接到一个函数。在main文件中增加代码如下:

import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

程序可以正常运行了。