bazel编译im2txt的问题

问题:

原本可以正常运行的程序,出现找不到tensorflow的问题。打印出来sys.version和sys.path,发现python版本并不是conda环境的版本

(tensorflow) yuany@hp-obelisk-desktop:~/AI/im2txt$ sh training.sh
INFO: Analyzed 17 targets (1 packages loaded, 2 targets configured).
INFO: Found 17 targets...
INFO: Elapsed time: 0.062s, Critical Path: 0.01s
INFO: 0 processes.
INFO: Build completed successfully, 9 total actions
3.6.8 (default, Aug 20 2019, 17:12:48)
[GCC 8.3.0]
['/home/yuany/AI/im2txt/im2txt', '/home/yuany/AI/im2txt/bazel-bin/im2txt/train.runfiles', '/home/yuany/AI/im2txt/bazel-bin/im2txt/train.runfiles/im2txt', '/home/yuany/AI/im2txt/bazel-bin/im2txt/train.runfiles/bazel_tools', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
--------------------------------
Traceback (most recent call last):
  File "/home/yuany/AI/im2txt/bazel-bin/im2txt/train.runfiles/im2txt/im2txt/train.py", line 25, in <module>
    import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'

直接运行im2txt子目录下的python文件,却可以正确使用anaconda环境中的python2.7

(tensorflow) yuany@hp-obelisk-desktop:~/AI/im2txt$ python im2txt/train.py
2.7.16 |Anaconda, Inc.| (default, Aug 22 2019, 16:00:36)
[GCC 7.3.0]
['/home/yuany/AI/im2txt/im2txt', '/home/yuany/anaconda3/envs/tensorflow/lib/python27.zip', '/home/yuany/anaconda3/envs/tensorflow/lib/python2.7', '/home/yuany/anaconda3/envs/tensorflow/lib/python2.7/plat-linux2', '/home/yuany/anaconda3/envs/tensorflow/lib/python2.7/lib-tk', '/home/yuany/anaconda3/envs/tensorflow/lib/python2.7/lib-old', '/home/yuany/anaconda3/envs/tensorflow/lib/python2.7/lib-dynload', '/home/yuany/anaconda3/envs/tensorflow/lib/python2.7/site-packages', '/home/yuany/anaconda3/envs/tensorflow/lib/python2.7/site-packages/PIL']
--------------------------------
Traceback (most recent call last):
  File "im2txt/train.py", line 27, in <module>
    from im2txt import configuration
ImportError: No module named im2txt

所以怀疑是bazel编译出现了问题。因为training.sh中有这么两步跟bazel有关。

# Build the model.
#cd research/im2txt
bazel build -c opt //im2txt/...

# Run the training script.
bazel-bin/im2txt/train 
  --input_file_pattern="${MSCOCO_DIR}/train-?????-of-00256" 
  --inception_checkpoint_file="${INCEPTION_CHECKPOINT}" 
  --train_dir="${MODEL_DIR}/train" 
  --train_inception=false 
  --number_of_steps=1000000

最后找到的原因是bazel版本升级了,把默认的python版本换成了3。 用一个低一点儿的bazel版本编译,就可以正常运行了。详细的应该也可以通过设置编译参数来指定用python2,但我并没有尝试。

https://github.com/bazelbuild/bazel/issues/7359

As of 0.24, py_binary and py_test targets that do not specify a python_version attribute (formerly called default_python_version) will use PY3 instead of PY2 when --incompatible_py3_is_default is enabled. In addition, --incompatible_py3_is_default makes the host configuration use PY3 instead of PY2, unless --host_force_python=PY2 is given to override this behavior.

--incompatible_py3_is_default requires that --incompatible_allow_python_version_transitions is set true, or else an error is raised. This means that if you opt out of --incompatible_allow_python_version_transitions during the migration window, you should also opt out of --incompatible_py3_is_default.

 https://github.com/tensorflow/lingvo/issues/113

卸载bazel

rm -rf ~/.bazel
rm -rf ~/bin
rm -rf /usr/bin/bazel

注释~/.bashrc 中bazel的环境变量

原文地址:https://www.cnblogs.com/scarecrow-blog/p/11638258.html