2016-01-06 57 views
1

我無法使Python memory line profiler正常工作。每當我從內IPython中運行%mprun,我得到的錯誤Python%mprun引發TypeError

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 
+0

如果您嘗試'stream.write('文件名:'+ unicode(文件名)+'\ n \ n')'? – vrs

+0

錯誤是從'memory_profiler'代碼中拋出的,我主要想知道我是否做錯了什麼或者是否應該提交錯誤報告。 – user1018464

+0

對不起,我錯誤地輸出了'stream'作爲你自己腳本的一部分,所以我的建議是無效的。我個人認爲'mpruntest'沒有任何問題。可能是一個錯誤 – vrs

回答

1

這是涉及到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