Macbook安裝Python最新版本、GUI開發環境、影象處理、視訊處理環境詳解
1、安裝
有兩種安裝方法:
方法一:從Mac自帶的python安裝,命令如下:
$brewinstall python
如果出錯的話前面加上sudo
方法一安裝的是python2.7
方法二:從官網下載安裝最新版本(本次安裝的內容)
官網地址:https://www.python.org/download,下載安裝最新版的python,安裝簡單,一路點選OK;不便之處是後續解除安裝維護需要手動進行。
可以用這個命令檢視python3安裝的位置:
$which python3
安裝完成後在終端中鍵入python3來驗證安裝是否成功.
開啟Mac終端,輸入“python3”,回車,檢視是否是自己裝的Python版本(如果Mac系統版本是10.8—10.11,預設自帶的是Python2.7)。輸入Python3後,看到顯示【>>>】,說明已經在Python的互動環境中了,可以正常使用。
這裡安裝的是pip3。
安裝了python3之後,會有pip3
注意:
1. 使用pip install XXX
新安裝的庫會放在這個目錄下面
python2.7/site-packages
2. 使用pip3 install XXX
新安裝的庫會放在這個目錄下面
python3.6/site-packages
如果使用python3執行程式,那麼就不能import python2.7/site-packages中的庫
2、 安裝pip3
pip3 是一個安裝和管理 Python3 包的工具,pip 是一個安裝和管理 Python 包的工具,python安裝包的工具有easy_install,setuptools,pip,distribute等。distribute是setuptools的替代品,是對標準庫disutils模組的增強,我們知道disutils主要是用來更加容易的打包和分發包,特別是對其他的包有依賴的包。distribute被建立是因為Setuptools包不再維護了。而pip/pip3是easy_install的替代品。
2.1 先獲取pip安裝指令碼:
$wget https://bootstrap.pypa.io/get-pip.py
如果沒有安裝wget可以去這裡:https://ftp.gnu.org/gnu/wget/下載
- 用解壓工具解壓 :wget-1.9.1.tar.gz
- cd 進入到解壓的目錄
- 命令列輸入:brew install wget
可以輸入wget www.baidu.com 測試是否安裝成功
將所有內容複製下來,新建get-pip.py檔案,將內容拷貝貼上進去.
2.2.安裝pip3
執行命令列:
$ sudo python3 get-pip.py
執行出錯:
The directory '/Users/xsnai/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo,you may want sudo's -H flag.
換成下列命令:
$sudo -H python3 get-pip.py
Requirement already up-to-date: pip in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
執行成功!
終端命令列輸入pip3檢測。
檢測結果:
Usage: pip <command> [options] Commands: install Install packages. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. check Verify installed packages have compatible dependencies. search Search PyPI for packages. wheel Build wheels from your requirements. hash Compute hashes of package archives. completion A helper command used for command completion. help Show help for commands. General Options: -h,--help Show help. --isolated Run pip in an isolated mode,ignoring environment variables and user configuration. -v,--verbose Give more output. Option is additive,and can be used up to 3 times. -V,--version Show version and exit. -q,--quiet Give less output. Option is additive,and can be used up to 3 times (corresponding to WARNING, ERROR,and CRITICAL logging levels). --log <path> Path to a verbose appending log. --proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port. --retries <retries> Maximum number of retries each connection should attempt (default 5 times). --timeout <sec> Set the socket timeout (default 15 seconds). --exists-action <action> Default action when a path already exists: (s)witch,(i)gnore,(w)ipe,(b)ackup,(a)bort. --trusted-host <hostname> Mark this host as trusted,even though it does not have valid or any HTTPS. --cert <path> Path to alternate CA bundle. --client-cert <path> Path to SSL client certificate,a single file containing the private key and the certificate in PEM format. --cache-dir <dir> Store the cache data in <dir>. --no-cache-dir Disable the cache. --disable-pip-version-check Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
2.3.修改pip3源
國內由於網路原因,使用pip3或和pip安裝一些模組會特別慢甚至無法下載,因此我們需要修改源到國內的一些映象地址,特別感謝國內無私奉獻的組織~
首先進入HOME路徑:
cd ~
建立.pip目錄:
mkdir .pip
建立pip.conf檔案:
touch pip.conf
大家可以用自己喜歡的編輯器開啟pip.conf檔案,我現在使用的時v2ex的源,所以新增:
[global]
index-url = http://pypi.v2ex.com/simple
大家可以把index-url的值設定為自己實際源的地址.
至此pip3源修改成功,以後使用pip安裝模組時都會從這個源去下載安裝,大家可以自行測試一下.
pip/pip3源使用國內映象,加快下載速度和安裝成功率
國內有如下下載源:
清華:https://pypi.tuna.tsinghua.edu.cn/simple
阿里雲:http://mirrors.aliyun.com/pypi/simple/
中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
華中理工大學:http://pypi.hustunique.com/
山東理工大學:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
臨時使用:
可以在使用pip/pip3的時候加引數-i https://pypi.tuna.tsinghua.edu.cn/simple
如:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider
這樣就會從清華這邊的映象去安裝pyspider庫。
永久修改,一勞永逸:
Linux下,修改 ~/.pip/pip.conf (沒有的話就建立一個資料夾及檔案。資料夾要加“.”,表示是隱藏資料夾)
內容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
windows下,直接在user目錄中建立一個pip目錄,如:C:\Users\xx\pip,新建檔案pip.ini。內容同上。
3、python3圖片處理
PIL(Python Imaging Library)影象處理模組,在python3.X已經替換為pillow模組(文件:http://pillow.readthedocs.org/en/latest/)。
直接使用pip3 install pillow即可安裝模組,匯入時使用from PIL import Image.
命令列如下:
$pip3 install pillow
執行過程如下:
Collecting pillow Downloading Pillow-4.3.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.5MB) 100% |████████████████████████████████| 3.6MB 19kB/s Collecting olefile (from pillow) Downloading olefile-0.44.zip (74kB) 100% |████████████████████████████████| 81kB 13kB/s Building wheels for collected packages: olefile Running setup.py bdist_wheel for olefile ... done Stored in directory: /Users/xsnai/Library/Caches/pip/wheels/20/58/49/cc7bd00345397059149a10b0259ef38b867935ea2ecff99a9b Successfully built olefile Installing collected packages: olefile,pillow Successfully installed olefile-0.44 pillow-4.3.0
4、影象中文字識別
4句程式碼實現漢字識別
from PIL import Image import pytesseract text=pytesseract.image_to_string(Image.open('denggao.jpeg'),lang='chi_sim') print(text)
需要先安裝兩個包,如果沒有安裝的話。
pip3 install PIL pip3 install pytesseract
還需要下載中文語言包tesseract-ocr
更多關於Mabook安裝Python最新版本(3.6.4)、GUI開發環境、影象處理、視訊處理環境請檢視下面的相關連結