3

version: 0.50.0

In the official documentation. there is a link to this blog which give me instruction of how to make per-function memory usage plot. but when I try to run the exactly code in this post.

test1.py

import time

@profile
def test1():
    n = 10000
    a = [1] * n
    time.sleep(1)
    return a

@profile
def test2():
    n = 100000
    b = [1] * n
    time.sleep(1)
    return b

if __name__ == "__main__":
    test1()
    test2()

command is:

mprof run test1.py

I get this error:

Traceback (most recent call last):

File "test.py", line 3, in @profile NameError: name 'profile' is not defined

this is very strange, because there is the quotes from official :

Warning

If your Python file imports the memory profiler from memory_profiler import profile these timestamps will not be recorded. Comment out the import, leave your functions decorated, and re-run.

So if I want to have the per-function memory usage plot, I need comment out from memory_profiler import profile, but when I comment out it, there is a error.

1
  • This is a bug in v0.50, and should be fixed in the current v0.51 Commented Jan 6, 2018 at 19:36

1 Answer 1

5

Documentation is not really up-to-date, try with mprof run --python python3 test1.py (having import commented out), it seems to work for me, but generates output to file only and does not write to stdout at all.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. it dows works. I also file a bug at github. I will share your answer, maybe can help them figure out the problem.
In case anyone come here with the same issue trying to run pytest, it works; Instead of python3 use pytest mprof run --python pytest path_to_test_file

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.