文章首發於個人我的博客猿人學Python,你也能夠關注個人我的公衆號,與50000好友一塊兒學習。python
方法一 本身編譯安裝:react
# 安裝編譯必需的軟件包 sudo apt install build-essential checkinstall sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev # 下載並解壓Python 3.6的源代碼 wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz tar xvf Python-3.6.0.tar.xz #編譯安裝 cd Python-3.6.0/ ./configure sudo make altinstall
make altinstall 命令會跳過建立symlink的步驟,所以/usr/bin/python仍然指向系統自帶的老版本python,不至於把Ubuntu系統搞崩潰。linux
方法二 使用PPA源進行安裝git
這是一個省事兒的方法,適合懶人用。github
sudo add-apt-repository ppa:jonathonf/python-3.6 sudo apt update sudo apt install python3.6 python3.6-dev
Ubuntu系統默認的Python是2.7,爲了使多個Python版本共存,咱們使用virtualenv/virtualenvwrapper來管理不一樣的Python版本和相應的軟件包。
virtualenvwrapper是virtualenv的擴展,使得管理虛擬環境更加方便。sql
sudo pip install virtualenv virtualenvwrapper
配置 virtualenvwarpperbash
默認virtualenvwrapper安裝在/usr/local/bin下面,實際上須要運行virtualenvwrapper.sh文件才行;因此須要先進行配置一下:app
建立虛擬環境管理目錄: mkdir $HOME/.virtualenvs
在~/.bashrc中添加行:函數
export VIRTUALENV_USE_DISTRIBUTE=1 # 老是使用 pip/distribute export WORKON_HOME=$HOME/.virtualenvs # 全部虛擬環境存儲的目錄 if [ -e $HOME/.local/bin/virtualenvwrapper.sh ];then source $HOME/.local/bin/virtualenvwrapper.sh else if [ -e /usr/local/bin/virtualenvwrapper.sh ];then source /usr/local/bin/virtualenvwrapper.sh fi fi export PIP_VIRTUALENV_BASE=$WORKON_HOME export PIP_RESPECT_VIRTUALENV=true
啓動 virtualenvwrapper: source ~/.bashrc 獲得以下輸出:post
veelion@gtx:~/opencv$ source ~/.bashrc virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/premkproject virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postmkproject virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/initialize virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/premkvirtualenv virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postmkvirtualenv virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/prermvirtualenv virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postrmvirtualenv virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/predeactivate virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postdeactivate virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/preactivate virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postactivate virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/get_env_details
使用方法
使用virtualenvwrapper —help 查看全部命令,經常使用的有:
建立Python3.6的虛擬環境
veelion@gtx:~$ mkvirtualenv -p python3.6 py3.6 Running virtualenv with interpreter /usr/bin/python3.6 Using base prefix '/usr' New python executable in /home/veelion/.virtualenvs/py3.6/bin/python3.6 Also creating executable in /home/veelion/.virtualenvs/py3.6/bin/python Installing setuptools, pip, wheel...done. virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/predeactivate virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/postdeactivate virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/preactivate virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/postactivate virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/get_env_details
安裝numpy
workon py3.6 pip install numpy
1. 安裝編譯依賴的軟件包
# 安裝讀寫不一樣圖片類型的庫: sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev # 安裝讀取視頻格式的庫: sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev # 安裝GTK庫以使用OpenCV的GUI: sudo apt-get install libgtk2.0-dev # 安裝用來優化一些OpenCV內部函數的庫,好比矩陣操做: sudo apt-get install libatlas-base-dev gfortran liblapacke-dev
2. 編譯OpenCV
下載並解壓源碼:
wget https://github.com/opencv/opencv/archive/3.2.0.tar.gz -O opencv.tar.gz wget https://github.com/opencv/opencv_contrib/archive/3.2.0.tar.gz -O opencv_contrib.tar.gz tar zxf opencv.tar.gz tar zxf opencv_contrib.tar.gz
編譯:
cd opencv-3.2.0 mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D WITH_IPP=OFF \ -DBUILD_opencv_dnn=OFF \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib-3.2.0/modules \ -D BUILD_EXAMPLES=OFF .. make -j4 # 編譯時間好長啊,這仍是四核一塊兒編譯呢
安裝
編譯沒有出錯的話,就能夠安裝了:
sudo make install sudo ldconfig
以上命令會安裝OpenCV的python模塊到:
(py3.6) veelion@gtx:~/opencv/opencv-3.2.0/build$ ls /usr/local/lib/python3.6/site-packages/ cv2.cpython-36m-x86_64-linux-gnu.so
把以上.so文件連接到py3.6虛擬環境下:
(py3.6) veelion@gtx:~$ cd ~/.virtualenvs/py3.6/lib/python3.6/site-packages/ (py3.6) veelion@gtx:~/.virtualenvs/py3.6/lib/python3.6/site-packages$ ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
測試OpenCV的安裝
veelion@gtx:~$ workon py3.6 (py3.6) veelion@gtx:~$ python Python 3.6.1 (default, Mar 22 2017, 10:05:53) [GCC 5.4.1 20170304] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.2.0'