属性错误:“模块”对象没有属性“maketrans”;运行 cProfile 时

发布于 2024-11-04 23:32:10 字数 1130 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情定在深秋 2024-11-11 23:32:10

正如 Python 模块教程 所解释的:

实际上,模块是在变量 sys.path 给出的目录列表中搜索的,该变量是从包含输入脚本(或当前目录)、PYTHONPATH 和安装相关默认值的目录初始化的。这使得 Python 程序知道自己在做什么,可以修改或替换模块搜索路径。请注意,由于包含正在运行的脚本的目录位于搜索路径上,因此脚本不能与标准模块同名,这一点很重要,否则 Python 在导入该模块时会尝试将脚本作为模块加载。< /p>

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

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

As the Python tutorial on modules explains:

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 does import string. Your script is named string.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 a maketrans module. That's what the error tells you.

(The same error should occur if you just run the script without profiling.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文