安装Paddlepaddle import paddle.fluid as fluid报错:ImportError: /opt/ros/kinetic/lib/python2.7/dist-pack

在配置Ubuntu16.04环境下的paddlepaddle时经常会遇到安装成功后,import paddle.fluid as fluid报错。报错截图如下:(其实是两个问题python2.7cv2文件和paddle下的__init__.py)

>>> import paddle.fluid as fluid
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/home/pac/.local/lib/python3.7/site-packages/paddle/__init__.py", line 39, in <module>
    from . import incubate
  File "/home/pac/.local/lib/python3.7/site-packages/paddle/incubate/__init__.py", line 15, in <module>
    from . import hapi
  File "/home/pac/.local/lib/python3.7/site-packages/paddle/incubate/hapi/__init__.py", line 16, in <module>
    from . import vision
  File "/home/pac/.local/lib/python3.7/site-packages/paddle/incubate/hapi/vision/__init__.py", line 15, in <module>
    from . import transforms
  File "/home/pac/.local/lib/python3.7/site-packages/paddle/incubate/hapi/vision/transforms/__init__.py", line 15, in <module>
    from . import transforms
  File "/home/pac/.local/lib/python3.7/site-packages/paddle/incubate/hapi/vision/transforms/transforms.py", line 18, in <module>
    import cv2


安装Paddlepaddle import paddle.fluid as fluid报错:ImportError: /opt/ros/kinetic/lib/python2.7/dist-pack_第1张图片
因为Ubuntu16.04默认Python环境是,Python2.7.12和Python3.5.2,理论上按照paddle官网的解释是可以支持,3.5.1+版本的Python,但是出现了这种错误后,咨询了官方交流群的管理,他建议我把版本换成3.7(另外在18.04的3.6.9则不会遇到这种问题)至此我一直以为是Python3的问题,其实不然。
上图可见,在Python3.7.7下还是会import失败。

于是我继续在交流群询问,也没有好的解决办法。直到看到了之前见过一次但是没有认真看的帖子解决import cv2的错误

我大概有解决思路了,首先解决在Python2.7下面的这个问题:
在安装好paddlepaddle的环境下(pip list指令可以查询),打开终端依次输入:

python3
import sys
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')

此时再去import paddle.fluid as fluid
会有如下报错:

>>> import paddle.fluid as fluid
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/home/pac/.local/lib/python3.7/site-packages/paddle/__init__.py", line 35, in <module>
    batch = batch.batch
NameError: name 'batch' is not defined

此时按照上面的路径打开__init__.py文件,在home目录下/.local/lib/python3.7/site-packages/paddle/init.py(可以用Ctrl+h打开查看隐藏文件.local)

安装Paddlepaddle import paddle.fluid as fluid报错:ImportError: /opt/ros/kinetic/lib/python2.7/dist-pack_第2张图片这时候打开__init__.py文件,如下图:

安装Paddlepaddle import paddle.fluid as fluid报错:ImportError: /opt/ros/kinetic/lib/python2.7/dist-pack_第3张图片
找到,图中我8.1号的注释,也就是原本代码为:

batch = batch.batch

的地方,用#将这一行注释掉。

至此,在Python3下再次运行 :

import paddle.fluid as fluid

不会出现报错,再运行:

 fluid.install_check.run_check()

发现paddlepaddle安装成功,至此问题解决!
在这里插入图片描述

你可能感兴趣的:(paddle报错,python,linux,ubuntu,paddlepaddle)