CentOS下安装Python3

CentOS下安装Python3

下载

CentOS 7默认安装python2.7,默认指令python上指向python2.7的,而且python2.7原文件就在/usr/bin/目录下

[cjp@localhost ~]$ which python
/usr/bin/python

[cjp@localhost bin]$ ls -al|grep python2
lrwxrwxrwx.  1 root root           9 3月  10 01:57 python -> python2.7
lrwxrwxrwx.  1 root root           9 2月  28 06:37 python2 -> python2.7
-rwxr-xr-x.  1 root root        7136 11月  6 2016 python2.7

因此要安装python3的话,得从python官网上下载相应版本的安装包

$ wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz

默认下载目录就当前路径

解压

# 一步到位
$ tar -xvJf  Python-3.6.6.tar.xz

# 分步
$ xz -d Python-3.6.6.tar.xz      # 解压成 Python-3.6.2.tar
$ tar xvf Python-3.6.6.tar

解压结果同样在当前路径

配置

需要制定后续编译与安装的制定路径

$ ./configure --prefix=/usr/local/python

gcc

[root@master ~]#./configure --prefix=/usr/local/python

checking build system type... i686-pc-linux-gnu

checking host system type... i686-pc-linux-gnu
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux
checking for --without-gcc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/src/pythonSoft/Python-3.6.6':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

由于本机缺少gcc编译环境

通过yum安装gcc编译环境:yum install -y gcc

$ yum install -y gcc

sudo权限

使用新建账户没发直接执行上面的配置语句,需要root权限,然而sudo还需要针对用户进行添加

$ sudo yum install -y gcc

修改/etc/sudoers,为当前账户添加sudo权限,使用vim

$ vim /etc/sudoers # 进入编辑模式,找到这一 行:"root ALL=(ALL) ALL"在起下面添加"xxx ALL=(ALL) ALL"(这里的xxx是你的用户名),然后保存退出

vim

i:进行编辑

esc:退缩编辑

:x :保存并退出

:x!:强制执行

编译

$ cd Python-3.6.6
$ make

安装

$ make install

因为安装涉及修改/usr/local/,必须使用sudo

$ sudo make install

安装过程提示缺少相应的包,使用yum进行安装

$ yum -y install zlib*

再重新安装即可

$ sudo make install

添加软链接

$ ln -s /usr/local/python/bin/python3.6  /usr/bin/python3
$ ln -s /usr/local/python/bin/pip3  /usr/bin/pip3

这样就同时可以使用python2和python3

pip安装出错,找不到SSL

https://blog.csdn.net/jeryjeryjery/article/details/77880227

$ cd Python-3.6.6
$ ./configure --with-ssl
$ make
$ sudo make install

但是这样的话,之前的 ./configure prefix=/usr/local/python就被覆盖了,改为

$ cd Python-3.6.6
$ ./configure --prefix=/usr/local/python --with-ssl
$ make
$ sudo make install

失败,需要提前安装openssl-devel

$ sudo yum install openssl-devel

再重新执行上面的语句

安装virtualenv和virtualenvwrapper

$ pip3 install virtualenv
$ pip3 install virtualenvwarpper

配置virtualenv软链接

因为virtualenvwarpper之后的指令需要使用到virtualenv的指令;因为python3编译安装时说我们自己自定的路径:/usr/local/python,所以之后使用pip安装的virtualenv并没有把默认指令安装在/usr/local/bin中,所以需要自己添加软链接。

如果最初安装python3时没有指定路径的话,或默认安装在/usr/local/下面,自动创建一个python3.6的文件夹,而且对应的指令文件都会在/usr/local/bin/里面,对应之后使用pip安装的virtualenv,其指令文件应该也是会在/usr/local/bin/里面;很方便,但文件就分离了,强迫症不允许。

配置virtualenvwrapper

不使用软链接,也无法使用,因为virtualenvwrapper的所有指令在一个sh文件中,而不是分散的指令文件;

所以使用.bash_profile配置,针对个人用户,不在root,同时每次修改完要重启一个该用户的bash才生效

# 设置环境变量,以后创建的虚拟环境均在此路径中
export WORKON_HOME=~/pyEnv
# 指定默认的python编译器版本
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
# 激活virtualenvwrapper.sh
source /usr/local/python/bin/virtualenvwrapper.sh 

额外

关于“.bash_profile”和“.bashrc”区别的总结

关于“交互式-非交互式”与“登录-非登陆”shell的总结

$ pip freeze > requirements.txt
$ pip install -r requirements.txt

Sqlit3 驱动

https://blog.csdn.net/blueheart20/article/details/79614505

yum install sqlite-devel

重新编译python安装,不需额外加配置信息

原文地址:https://www.cnblogs.com/stream886/p/10474786.html