Ubuntu 16.04安装Python 3.6 + OpenCV的虚拟环境

Python 3.6 发布已经几个月了,这次安装一下练练手,熟悉一下新特性。

1. Ubuntu 14.04/16.04安装Python 3.6

方法一 自己编译安装:

# 安装编译必需的软件包
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系统搞崩溃。

方法二 使用PPA源进行安装

这是一个省事儿的方法,适合懒人用。

sudo add-apt-repository ppa:jonathonf/python-3.6

sudo apt update

sudo 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 ~/.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 查看所有命令,常用的有:

  • 创建基本环境:mkvirtualenv [环境名]
  • 删除环境:rmvirtualenv [环境名]
  • 激活环境:workon [环境名]
  • 退出环境:deactivate
  • 列出所有环境:workon 或者 lsvirtualenv -b

创建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

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.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'

未经允许不得转载:996ICU » Ubuntu 16.04安装Python 3.6 + OpenCV的虚拟环境

赞 (0) 打赏