我遇到了一个奇怪的python模块导入问题。
当我试图导入沸腾管模块时,
from boilerpipe.extract import Extractor我有个例外:
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/boilerpipe-1.2.0-py2.7.egg/boilerpipe/extract/ __init__.py", line 2, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 1140, in <module>
import ssl
File "/usr/lib/python2.7/ssl.py", line 58, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'我在互联网上搜索过,并说在Python2.6中,'str‘模块被重命名为'string’模块。因此,这看起来像是代码库中没有正确导入"string“模块的地方。
然而,真正奇怪的是,当我从目录运行python代码并运行相同的代码(使用python shell或使用python pyfile.py)时,它工作得很好!不再有重要错误。
所以我有点困惑。有人能给我一点提示吗?
谢谢!
发布于 2013-05-30 19:28:36
sys.path中的其他一些脚本名为"string.py“,它隐藏着stdlib模块。
发布于 2013-05-30 19:30:20
重复检查,以确保您没有导入的文件string.py。
要调试这一点,请放置以下位置:
import sys
raise Exception("string module: %r" %(sys.modules.get("string"), ))这将告诉您导入了哪个字符串模块(或者如果它显示None,还没有导入string模块)。
https://stackoverflow.com/questions/16844296
复制相似问题