Python%mprun抛出TypeError

时间:2016-01-06 16:33:36

标签: python memory-profiling

我无法让Python memory line profiler工作。每当我从IPython中运行%mprun时,我都会收到错误

TypeError: unicode argument expected, got 'str'

我使用了我能想象到的最简单的例子。

In [1]: %load_ext memory_profiler
In [2]: from mpruntest import func
In [3]: A = rnd.randn(1000, 1000)
In [4]: %mprun -f func func(A)

给出:

/Users/markvdw/anaconda/lib/python2.7/site-packages/memory_profiler.pyc in show_results(prof, stream, precision)
    600                                  'Line Contents')
    601 
--> 602         stream.write('Filename: ' + filename + '\n\n')
    603         stream.write(header + '\n')
    604         stream.write('=' * len(header) + '\n')

TypeError: unicode argument expected, got 'str'

mpruntest包含:

import numpy as np

def func(x):
    y = x**2.0
    z = np.exp(y)
    return z

1 个答案:

答案 0 :(得分:2)

这是一个与Python 2相关的错误。我刚刚发布了一个新版本的memory_profiler(0.41),可以解决这个问题。输出现在是您所期望的:

Line #    Mem usage    Increment   Line Contents
================================================
    11     42.5 MiB      0.0 MiB   def func(x):
    12     50.2 MiB      7.6 MiB       y = x**2.0
    13     57.8 MiB      7.6 MiB       z = np.exp(y)
    14     57.8 MiB      0.0 MiB       return z