adam
adam

Reputation: 3578

Python site-package module not found

I'm having troubles mainly from my inexperience with python and OSX, and trying to understand how it all works.

End goal is to get this module to run: http://pypi.python.org/pypi/memory_profiler, except it never finds the module.

So for starters I did the easy_install and everything installed fine from what I can tell:

easy_install -U memory_profiler # pip install -U memory_profiler

Next I created an example.py file just to get the ball rolling:

@profile
def my_func():
    return 2

if __name__ == '__main__':
    my_func()

and tried to run it, but got this error:

$ python example.py

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    @profile
NameError: name 'profile' is not defined

This isn't so much a question about the memory_profiler module, but more about what am I doing wrong and have configured incorrectly? I'm using OSX 10.8.2 with Python 2.7.

This is what my "which python" states:

/Library/Frameworks/Python.framework/Versions/Current/bin/python

Since its a symbolic link, when I go to the original its at:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python

Where I'm confused is that easy_install correctly put the memory_profiler.py file in this folder:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

And I have the understanding that when python runs, it checks for modules in the PYTHONPATH and in the site-packages. (??)

But if the module is in the site-packages folder, why doesn't the example.py work? Also, if modules in the site-packages folder should be a part of the path, I figured I could at least run the memory_profiler.py just to see if it gets ran by python, but got this error instead:

python memory_profiler.py
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: 
can't open file 'memory_profiler.py': [Errno 2] No such file or directory

This also confuses me, because its stating that python is in: ../2.7/Resources/Python.app/Contents/MacOS/Python, but I thought it was supposed to be ../2.7/bin/python, and shouldn't it be checking for the memory_profiler.py file in ../2.7/lib/python2.7/site-packages?

Also, why do both of these folders exist, and what's the difference?

/System/Library/Frameworks/Python.framework
/Library/Frameworks/Python.framework

I'm missing a big piece of the puzzle, so any help to point in the right direction would be very appreciated.

Update:

I was leaving out:

from memory_profiler import profiler

Probably most of my bonehead issue, but now I get this error:

Traceback (most recent call last):
File "example.py", line 1, in <module>
from memory_profiler import profiler
ImportError: cannot import name profiler

Upvotes: 2

Views: 4963

Answers (3)

구마왕
구마왕

Reputation: 498

I had similar issue but it turned out that I installed memory_profiler into python3 not 2.
And specifying the python version solved the issue.

>>> python -m memory_profiler test.py
/usr/bin/python: No module named memory_profiler
>>> python3 -m memory_profiler test.py
Filename: test.py

Line #    Mem usage    Increment  Occurences   Line Contents
============================================================
     1   19.930 MiB   19.930 MiB           1   @profile
     2                                         def my_func():
     3   27.406 MiB    7.477 MiB           1       a = [1] * (10 ** 6)
     4  180.031 MiB  152.625 MiB           1       b = [2] * (2 * 10 ** 7)
     5   27.586 MiB -152.445 MiB           1       del b
     6   27.586 MiB    0.000 MiB           1       return a

Upvotes: 0

Fabian Pedregosa
Fabian Pedregosa

Reputation: 6549

In previous versions of line_profiler you had to run it from the command line as @johnthexii points out. Running it from the command line is still the recommended way of running the profiler (because it sets some hooks in the interpreter that are not set otherwise), but it is now possible to also import the decorator as

from memory_profiler import profile

Upvotes: 2

John
John

Reputation: 13699

I assume you have your import statments? from memory_profiler import profiler... From reading your question it appears that you think that python automatically imports everything from all modules in the PYTHONPATH, which it doesn't because that would take up too much memory and what if two modules have the same function.

Edit

So it appears the only way that the @profiler decorator works is if you run the program from the commandline...

python -m memory_profiler example.py

If you want to use memory_profiler from within the script refer to this example. https://github.com/fabianp/memory_profiler/blob/master/examples/plot_memory.py

Upvotes: 2

Related Questions