使用Python的一些記錄
本人經常使用的是 python2.7.x
常用的部分
1. 文件開頭聲明
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2. 簡單的Python2服務器
python -m SimpleHTTPServer <port>
3. 單步調試Python程序
python -m pdb <program>
4. 性能分析Profile工具簡單的使用
- Python Profile 工具性能分析http://ju.outofmemory.cn/entry/46805
- 關於Python Profilers性能分析器http://www.cnblogs.com/btchenguang/archive/2012/02/03/2337112.html
第二篇是譯文,原文是 https://docs.python.org/3/library/profile.html#cProfile
一些問題的解決
1. file.write 中包含非ascii字符
StackOverflow: Writing Unicode text to a text file?
2. Mac 下升級python後pip不正常
本來裝的是 python2.7.10, 後來升級是用的
brew install --upgrade python
Mac系統可以有系統的python和用戶的python同時存在,分別是在/usr/bin
和/usr/local/bin
下面。然後運行程序可能會提示
import XXX: module scipy not found
但是運行
pip install scipy
又提示requirements satisfied
。
刪除又重裝pip還是不能解決問題。
後來是參考了幾篇文章和問題的回答才意識到錯誤
1. python升級引起的pip執行錯誤 http://blog.csdn.net/iefreer/article/details/8086834
2. which python和pip在不同位置問題https://www.v2ex.com/t/94613
3. /usr/bin/pip: No such file or directory http://cheng.logdown.com/posts/2015/06/14/-usr-bin-pip-no-such-file-or-directory)
4. No such file or directory: ‘/usr/local/bin/pip’http://stackoverflow.com/questions/41275541/no-such-file-or-directory-usr-local-bin-pip
brew安裝的python是在/usr/local/bin
, 但是pip的安裝是在/usr/bin
, 總之兩個東西的版本不對應。如果通過上面參考中的whereis python
和whereis pip
可以看到區別,那麼就是這個問題了。
我用brew uninstall python
卸載了用戶版本的python,更新了pip就恢復正常了。