属性错误:“模块”对象没有属性“maketrans”;运行 cProfile 时
使用 python 2.7 我得到这个错误:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/lib/python2.7/cProfile.py", line 199, in <module>
main()
File "/usr/lib/python2.7/cProfile.py", line 165, in main
from optparse import OptionParser
File "/usr/lib/python2.7/optparse.py", line 77, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 32, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 74, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
在运行这个简单的代码时:
def blah():
orig = ""
for i in range(1000000):
orig += "zim";
blah()
使用这个调用:
$ python -m cProfile string.py
我正在使用 Ubuntu Natty Narwhal,并安装了 python-profiler 包(我不知道这是否有必要)。
Using python 2.7 I get this error:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/lib/python2.7/cProfile.py", line 199, in <module>
main()
File "/usr/lib/python2.7/cProfile.py", line 165, in main
from optparse import OptionParser
File "/usr/lib/python2.7/optparse.py", line 77, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 32, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 74, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
while running this simple code:
def blah():
orig = ""
for i in range(1000000):
orig += "zim";
blah()
using this call:
$ python -m cProfile string.py
I'm using Ubuntu Natty Narwhal, and installed the package python-profiler (I don't know if this is necessary).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Python 模块教程 所解释的:
textwrap
确实导入字符串
。您的脚本名为string.py
并且在搜索路径上首先出现(或至少在 stdlib 目录之前),因此它被导入。但它没有定义预期的函数和常量,例如它没有maketrans
模块。这就是错误告诉你的。(如果您只运行脚本而不进行分析,也会出现相同的错误。)
As the Python tutorial on modules explains:
textwrap
doesimport string
. Your script is namedstring.py
and comes first (or at least before the stdlib directories) on the search path, so it is imported. But it doesn't define the functions and constants expected, e.g. it doesn't have amaketrans
module. That's what the error tells you.(The same error should occur if you just run the script without profiling.)