除非root用户,否则无法导入python模块

时间:2017-03-13 08:36:19

标签: python python-2.7 ubuntu python-import ubuntu-16.04

当我从普通用户打开python时,我无法在终端中导入python模块(虽然它适用于root用户):
我得到的错误是:

import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 180, in <module>
    from . import add_newdocs
  File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 58, in <module>
    from numpy.testing.nosetester import _numpy_tester
  File "/usr/lib/python2.7/dist-packages/numpy/testing/__init__.py", line 12, in <module>
    from . import decorators as dec
  File "/usr/lib/python2.7/dist-packages/numpy/testing/decorators.py", line 21, in <module>
    from .utils import SkipTest
  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 15, in <module>
    from tempfile import mkdtemp, mkstemp
  File "/usr/lib/python2.7/tempfile.py", line 32, in <module>
    import io as _io
  File "/home/praneeth/computervision/code/caffe-master/distribute/python/caffe/io.py", line 2, in <module>
    import skimage.io
  File "/home/praneeth/scikit-image/skimage/__init__.py", line 158, in <module>
    from .util.dtype import *
  File "/home/praneeth/scikit-image/skimage/util/__init__.py", line 1, in <module>
    from .dtype import (img_as_float, img_as_int, img_as_uint, img_as_ubyte,
  File "/home/praneeth/scikit-image/skimage/util/dtype.py", line 8, in <module>
    dtype_range = {np.bool_: (False, True),
AttributeError: 'module' object has no attribute 'bool_'

与所有模块一样,不仅仅是numpy。我已经尝试过在其他类似问题中提出的解决方案,但是他们还没有解决。例如: Cannot import a python module that is definitely installed (mechanize)

命令$PYTHONPATH的输出是:

  

/home/praneeth/computervision/code/caffe-master/distribute/python/caffe:/home/praneeth/computervision/code/caffe-master/python/caffe:/usr/lib/python2.7/dist-包/

此外,我的没有root权限的系统路径是:

import sys
sys.path
    ['', '/home/praneeth/computervision/code/caffe-master/distribute/python/caffe', '/home/praneeth/computervision/code/caffe-master/python/caffe', '/usr/lib/python2.7/dist-packages', '/home/praneeth', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/home/praneeth/scikit-image', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0']

EDIT1: 发现root用户的sys.path与没有root用户期望的那些caffe安装路径相同。以下是root用户的结果:

sudo python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/home/praneeth/scikit-image', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat']

1 个答案:

答案 0 :(得分:0)

终于解决了!!问题是因为在安装caffe时为python和其他一些版本的numpy单独安装numpy和scipy(使用不同的版本)。两个numpy和scipy相关库之间存在版本冲突,因此,python解释器无法导入它们。

这次再次正确地重新安装了caffe,没有任何版本冲突,问题得到了解决。

感谢您的帮助