Python不会导入模块

时间:2018-07-28 12:59:34

标签: python python-2.7

这是我得到的错误

Traceback (most recent call last):
  File "detectron/utils/subprocess.py", line 28, in <module> import numpy as np
  File "/usr/local/lib/python2.7/dist-packages/numpy/__init__.py", line 142, in <module> from . import add_newdocs
  File "/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module> from numpy.lib import add_newdoc
  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module> from .type_check import *
  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module> import numpy.core.numeric as _nx
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/__init__.py", line 74, in <module> from numpy.testing import _numpy_tester
  File "/usr/local/lib/python2.7/dist-packages/numpy/testing/__init__.py", line 10, in <module> from unittest import TestCase
  File "/usr/lib/python2.7/unittest/__init__.py", line 58, in <module> from .result import TestResult
  File "/usr/lib/python2.7/unittest/result.py", line 9, in <module> from . import util
  File "/usr/lib/python2.7/unittest/util.py", line 2, in <module> from collections import namedtuple, OrderedDict 

  ImportError: cannot import name namedtuple

这里的问题是模块已安装

$ ls /usr/lib/python2.7
    ...
    -rwxr--r-- 1 root root  27798 Dec  5  2017 collections.py
    -rwxr--r-- 1 root root  26059 Jul 17 17:40 collections.pyc
    ...

如果我直接从python导入,则可以正常工作:

$ python
    Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from collections import namedtuple, OrderedDict
    >>> 

$ PYTHONPATH就是这里

$ echo $PYTHONPATH
    /usr/lib/python2.7/

这是$ PATH的

$ echo $PATH
    /home/deep/bin:/home/deep/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/bin/python

这是python看到的

>>> import sys
>>> from pprint import pprint as p
>>> p(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',
 '/home/deep/.local/lib/python2.7/site-packages',
 '/home/deep/cocoapi/PythonAPI/Detectron',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/local/lib/python2.7/dist-packages/pycocotools-2.0-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages']
>>> 

我当时以为这是某种冲突,但我不知道如何...

$ sudo find / -name "collections.py"
    /home/deep/cocoapi/PythonAPI/Detectron/detectron/utils/collections.py
    /usr/local/lib/python2.7/dist-packages/hypothesis/searchstrategy/collections.py
    /usr/local/lib/python2.7/dist-packages/future/moves/collections.py
    /usr/local/lib/python2.7/dist-packages/matplotlib/collections.py
    /usr/lib/python2.7/collections.py

$ sudo find / -name "collections"
    /usr/lib/python3.5/collections

我没主意了;我不知道为什么不能导入此模块。 预先谢谢你。

*编辑:似乎

/home/deep/cocoapi/PythonAPI/Detectron/detectron/utils/collections.py

正在引起冲突。有什么方法可以确定调用的优先级,因此我可以先使用python库?

1 个答案:

答案 0 :(得分:2)

$ sudo find / -name "collections.py"
   /home/deep/cocoapi/PythonAPI/Detectron/detectron/utils/collections.py

我认为,这可能是罪魁祸首。

detectron软件包中有一个名为“集合”的模块。

我想,以某种方式在导入标准collections模块之前先导入了此collections模块。

所有模块在导入后均按名称存储在内存中。因此,当不同的软件包再次导入“集合”时,它将(由于名称冲突)得到错误的模块。并且因为它不包含namedtuple,所以会出现错误。

解决方法:

  • 可以重命名集合模块
  • 或确保始终将其与软件包一起导入(完全合格)

Ergo:

  • 请勿在模块中使用标准模块的名称,这很危险。