jupyter-notebook:在cell中运行python文件使用的python解释器和kernel不一致

如题,使用jupyter-notebook时,有如下代码:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# Make sure that caffe is on the python path:
caffe_root = '../'  # this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe

plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

import os
if not os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
    print("Downloading pre-trained CaffeNet model...")
    !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet

代码最后一行运行了download_model_binary.py,报错:

AttributeError: 'module' object has no attribute 'urlretrieve'

百度说是py3的urllib改了,需要加request,即urllib.request.urlretrieve,但我用的是py2啊!!!

jupyterdownload_model_binary.py均加入如下代码来查看python版本:

print sys.version_info[0] # 在download_model_binary.py中print语句提示缺少括号其实就已经证明两个python不一致了

发现jupyter输出的是2,download_model_binary.py输出的是3
即在cell中运行python文件使用的python解释器和kernel不一致。

解决方案:
!换成%run,后边再加个空格

%run ../scripts/download_model_binary.py ../models/bvlc_reference_caffenet
上一篇:Caffe训练:用shell脚本生成图片列表txt文件


下一篇:Mac 安装配置Caffe-CPU