文章首发于我的个人博客,你也可以关注我的个人公众号,与50000好友一起学习。
1. Ubuntu 14.04/16.04安装Python 3.6
方法一 自己编译安装:
# 安装编译必需的软件包sudo apt install build-essential checkinstallsudo 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.xztar xvf Python-3.6.0.tar.xz#编译安装cd Python-3.6.0/./configuresudo make altinstall
make altinstall 命令会跳过创建symlink的步骤,因此/usr/bin/python仍然指向系统自带的老版本python,不至于把Ubuntu系统搞崩溃。
方法二 使用PPA源进行安装
这是一个省事儿的方法,适合懒人用。
sudo add-apt-repository ppa:jonathonf/python-3.6sudo apt updatesudo apt install python3.6 python3.6-dev
2. 设置Python虚拟环境
Ubuntu系统默认的Python是2.7,为了使多个Python版本共存,我们使用virtualenv/virtualenvwrapper来管理不同的Python版本和相应的软件包。
virtualenvwrapper是virtualenv的扩展,使得管理虚拟环境更加方便。sudo pip install virtualenv virtualenvwrapper
配置 virtualenvwarpper
默认virtualenvwrapper安装在/usr/local/bin下面,实际上需要运行virtualenvwrapper.sh文件才行;所以需要先进行配置一下:
创建虚拟环境管理目录: 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 得到如下输出:
veelion@gtx:~/opencv$ source ~/.bashrcvirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/premkprojectvirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postmkprojectvirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/initializevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/premkvirtualenvvirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postmkvirtualenvvirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/prermvirtualenvvirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postrmvirtualenvvirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/predeactivatevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postdeactivatevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/preactivatevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/postactivatevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/get_env_details
使用方法
使用virtualenvwrapper —help 查看所有命令,常用的有:
- 创建基本环境:mkvirtualenv [环境名]
- 删除环境:rmvirtualenv [环境名]
- 激活环境:workon [环境名]
- 退出环境:deactivate
- 列出所有环境:workon 或者 lsvirtualenv -b
创建Python3.6的虚拟环境
veelion@gtx:~$ mkvirtualenv -p python3.6 py3.6Running virtualenv with interpreter /usr/bin/python3.6Using base prefix '/usr'New python executable in /home/veelion/.virtualenvs/py3.6/bin/python3.6Also creating executable in /home/veelion/.virtualenvs/py3.6/bin/pythonInstalling setuptools, pip, wheel...done.virtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/predeactivatevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/postdeactivatevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/preactivatevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/postactivatevirtualenvwrapper.user_scripts creating /home/veelion/.virtualenvs/py3.6/bin/get_env_details
安装numpy
workon py3.6pip install numpy
2. 编译安装OpenCV 3.2
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.gzwget https://github.com/opencv/opencv_contrib/archive/3.2.0.tar.gz -O opencv_contrib.tar.gztar zxf opencv.tar.gztar zxf opencv_contrib.tar.gz
编译:
cd opencv-3.2.0mkdir buildcd buildcmake -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 installsudo 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:~$ pythonPython 3.6.1 (default, Mar 22 2017, 10:05:53) [GCC 5.4.1 20170304] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import cv2>>> cv2.__version__'3.2.0'