一.python3配置:
使用的阿里云镜像ubantu 16.04,自带python3.5.2,但是没有pip3,和dev解释器,需要自行安装,而阿里的源比较老,没有这些组件,推荐更换清华的源
1、备份系统默认源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vi /etc/apt/sources.list
3、将以下内容替换到源文件(注意在清华大学网站上选择合适自己系统的版本)
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
sudo apt-get update
5、安装并更新pip3
apt-get install python3-pip python3-dev
pip3 install --upgrade pip
如果想要新版本的python,也可以安装新版本的python
下载解压并安装python3.6.4,指定路径为/usr/local/python36
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
tar zxvf Python-3.6.4.tgz
cd Python-3.6.4
#--prefix指定安装路径,也可以默认安装,不过默认安装对于系统中已经有2.7和3.5情况下,管理上会比较混乱,所以指定目录较为合适
./configure --prefix="/usr/local/python36" --enable-optimizations
make
make install
建立Python3的软链接,并将/usr/local/python36/bin加入bash_profile(临时环境,比修改etc文件要稳定)
ln -s /usr/local/python36/bin/python3 /usr/bin/python3
vi ~/.bash_profile
/usr/local/python36/bin
source ~/.bash_profile
如果是CentOS系统需要注意修改yum的python设置
修改yum配置文件,将python版本指向以前的旧版本
vi /usr/bin/yum
!/usr/bin/python2.7
修改urlgrabber-ext-down文件,更改python版本
vi /usr/libexec/urlgrabber-ext-down
!/usr/bin/python2.7
二.boost配置:
1.到官网下载最新版的boost并解压
pip自带的boost十分老,可能会无法支持pip安装的dlib版本的编译,推荐自行编译下boost下载地址:http://www.boost.org/users/history/version_1_66_0.html
tar zxvf boost_1_66_0.tar.gz
cd boost_1_66_0
2.安装
./bootstrap.sh --with-python=/usr/bin/python3
./b2
./b2 install
#要记得更新环境
ldconfig
测试boost库是否安装成功
//test1.cpp
#include <boost/thread.hpp>
#include <iostream>
void task1() {
std::cout << "This is task1!" << std::endl;
}
void task2()
{
std::cout << "This is task2!" << std::endl;
}
int main (int argc, char ** argv) {
using namespace boost;
thread thread_1 = thread(task1);
thread thread_2 = thread(task2);
thread_2.join();
thread_1.join();
return 0;
}
编译指令:
g++ test1.cpp -o test1 -lpthread -lboost_thread -lboost_system
./test1
#输出
This is task1!
This is task2!
三.dlib配置:
dlib是目前最常用的机器学习库, face_recognition是在dlib的基础之上进行再封装的一个python自带人脸识别库,两者都可以直接通过pip3安装, dlib无论pip安装还是编译都比较会耗时比较久
apt-get install cmake python-qt4
#安装dlib和face_tecognition的时候,pip3会自动安装numpy等必须的模组
pip3 install opencv-python
pip3 install dlib
#如果不需要face_recognition,则略过此步骤
pip3 install face_recognition
测试dlib库是否安装成功,在python3中import dlib,不报错即表示成功
python3
>>>import dlib
>>>
以上是pip方法的安装,如果行不通可以自行编译dlib
1.到官网下载最新版的dlib并解压
#安装bz解压,有则略过此步骤
yum -y install bzip2
tar -jxvf dlib-19.8.tar.bz2
cd dlib-19.8
# --no DLIB_USE_CUDA选项不使用cuda, --yes USE_AVX_INSTRUCTIONS表示启用AVX优化
#这些选项根据自身需求定制,一般情况下这2个是比较常见的设置
python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA
可能需要的前置依赖:
yum install python3-devel python-lxml numpy gcc gcc-c++ gtk2-devel libdc1394-devel libv4l-devel ffmpeg-devel gstreamer-plugins-base-devel libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel opencv opencv-python opencv-devel
四.tensorflow配置:
CPU版本:
pip3 install tensorflow
想要安装特定版本的tensorflow请用下面的命令:
pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-xxx.whl
如果出现:
>>>ImportError: No module named tensorflow
则分别先卸载protobuf和tensorflow再按顺序安装即可:
pip3 uninstall protobuf
pip3 uninstall tensorflow
pip3 install protobuf
pip3 install tensorflow
GPU版本:
这个略麻烦,需要先更新驱动,再安装CUDA和cudnn,请注意自己的显卡和CUDA,cudnn的版本对应关系!
1. 去Nvidia官网查看显卡是否支持CUDA:https://developer.nvidia.com/cuda-gpus
2. GPU版本的安装过程推荐看这篇博客,写得比较详尽:http://blog.csdn.net/ZWX2445205419/article/details/69429518?%3E
测试tensorflow是否安装成功:
python3
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
问题集合:
1.PyClass_Type符号错误
libboost_python3.so.1.66.0: undefined symbol: PyClass_Type
错误原因:python-dev版本不对,系统自带python2.7,相应的python-dev也是对应python2的,编译出来的boost也是python2风格,但是程序员却在python3中调用boost,导致符号错误
解决方法:安装python3-devel,重新走一遍流程即可
apt-get install python3-dev
2.pyconfig.h找不到
`fatal error: 'pyconfig.h' file not found`
错误原因:找不到python-dev,可能是1.pyconfig.h的路径没有加入到系统变量里;2.根本没有安装相应的python-dev
解决方法:1.全盘搜索下该文件,将路径加入到系统环境变量中即可;2.apt-get安装python-dev即可,安装不了请更换清华的源
find / -name pyconfig.h
/usr/include/python3.5m/pyconfig.h
# export include环境变量
export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/usr/include/python3.5m"
# 或者也可以修改/etc/ld.so.conf
vi /etc/ld.so.conf
/usr/include/python3.5m
ldconfig