我遇到麻煩主要來自我對Python和OSX的經驗不足,並試圖瞭解它是如何工作的。找不到Python站點包模塊
最終目標是讓模塊運行:http://pypi.python.org/pypi/memory_profiler,但它永遠不會找到該模塊。
所以對於初學者,我做了easy_install的一切安裝了什麼,我可以告訴罰款:
easy_install -U memory_profiler # pip install -U memory_profiler
接下來,我創建了一個example.py文件只是爲了讓球滾動:
@profile
def my_func():
return 2
if __name__ == '__main__':
my_func()
並試圖運行它,但得到了這個錯誤:
$ python example.py
Traceback (most recent call last):
File "example.py", line 2, in <module>
@profile
NameError: name 'profile' is not defined
這不是一個關於memory_prof的問題iler模塊,但更多關於我在做什麼錯誤,並配置不正確?我在Python 2.7中使用OSX 10.8.2。
這就是我的「這蟒蛇」規定:
/Library/Frameworks/Python.framework/Versions/Current/bin/python
自符號鏈接,當我去到原來的位置:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
當我感到困惑的是, easy_install正確地把memory_profiler.py文件放在這個文件夾中:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
而且我有一個理解,那就是當python運行時,它會檢查PYTHONPATH和站點包中的模塊。 (??)
但是,如果模塊位於site-packages文件夾中,爲什麼example.py不工作?另外,如果site-packages文件夾中的模塊應該是路徑的一部分,我想我至少可以運行memory_profiler.py來查看它是否被python運行,但是卻得到了這個錯誤:
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
這也令我困惑,因爲它聲明python在:../2.7/Resources/Python.app/Contents/MacOS/Python,但我認爲它應該是../2.7/bin/python,並且它不應該檢查../2.7/lib/python2.7/site-packages中的memory_profiler.py文件嗎?
此外,爲什麼這兩個文件夾都存在,有什麼區別?
/System/Library/Frameworks/Python.framework
/Library/Frameworks/Python.framework
我錯過了一大塊拼圖,所以任何幫助指向正確的方向將非常感激。
更新:
我離開了:
from memory_profiler import profiler
也許我最笨蛋的問題,但現在我得到這個錯誤:
Traceback (most recent call last):
File "example.py", line 1, in <module>
from memory_profiler import profiler
ImportError: cannot import name profiler
不,我完全錯過了。我剛剛更新了下一個錯誤的問題 – adam
要回答您的許多問題之一:'/ System/Library/Frameworks/Python.framework'包含Apple提供的系統Pythons(與'/ usr/bin/pythonx.x';'/ Library/Frameworks/Python.framework'包含你安裝的其他Python實例,例如,使用OS X的'python.org'安裝程序。它們通常鏈接到'/ usr/local/bin/pythonx.x'以及框架中版本化的bin目錄。 –