gpt4 book ai didi

python - 属性错误 : 'module' object has no attribute 'maketrans' while running cProfile

转载 作者:太空狗 更新时间:2023-10-30 00:57:40 26 4
gpt4 key购买 nike

使用 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 包(我不知道这是否有必要)。

最佳答案

作为Python tutorial on modules解释:

Actually, modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script (or the current directory), PYTHONPATH and the installation- dependent default. This allows Python programs that know what they’re doing to modify or replace the module search path. Note that because the directory containing the script being run is on the search path, it is important that the script not have the same name as a standard module, or Python will attempt to load the script as a module when that module is imported.

textwrap导入字符串。您的脚本名为 string.py 并且在搜索路径中排在第一位(或至少在 stdlib 目录之前),因此它被导入。但它没有定义预期的函数和常量,例如它没有 maketrans 模块。这就是错误告诉您的内容。

(如果您只是运行脚本而不进行分析,也会出现同样的错误。)

关于python - 属性错误 : 'module' object has no attribute 'maketrans' while running cProfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5889466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com