Python开发【初始篇】:Linux下安装Python3

Linux系统默认自带python2.6的版本,这个版本被系统很多程序所依赖,所以建议不要轻易删除,除非你能解决其他程序的依赖问题。如果使用最新的Python3需要进行编译安装源码包,这样就对系统默认的包没有任何影响。

  1. [root@test-c2c-console01 ~]# cat /etc/redhat-release
  2. CentOS release 6.6 (Final)
  3. [root@test-c2c-console01 ~]# uname -a
  4. Linux test-c2c-console01.bj 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
  5. [root@test-c2c-console01 ~]# python -V
  6. Python 2.6.6

Python官网下载pthon3源码包,https://www.python.org/

选择自己需要的版本

选择源码包

下载到本地然后上传到linux或者复制下载链接直接通过wget下载

  1. [root@test-c2c-console01 tools]# wget -q https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
  2. [root@test-c2c-console01 tools]# ll Python-3.5.2.tgz
  3. -rw-r--r-- 1 root root 20566643 Jun 26 2016 Python-3.5.2.tgz

解压并安装

  1. [root@test-c2c-console01 tools]# tar xf Python-3.5.2.tgz
  2. [root@test-c2c-console01 tools]# cd Python-3.5.2
  3. [root@test-c2c-console01 Python-3.5.2]# ./configure --prefix=/application/Python-3.5.2/ #指定安装目录
  4. [root@test-c2c-console01 Python-3.5.2]# make && make install

创建软连接

  1. [root@test-c2c-console01 Python-3.5.2]# cd /application/
  2. [root@test-c2c-console01 application]# ln -s Python-3.5.2/ Python3
  3. [root@test-c2c-console01 application]# ll
  4. total 4
  5. lrwxrwxrwx 1 root root 13 Mar 1 09:08 Python3 -> Python-3.5.2/
  6. drwxr-xr-x 6 root root 4096 Mar 1 09:03 Python-3.5.2

到此python3就安装好了,启动pyhon3。

  1. [root@test-c2c-console01 application]# /application/Python3/bin/python3
  2. Python 3.5.2 (default, Mar 1 2017, 09:02:01)
  3. [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>>

由于这样启动比较麻烦可以通过配置环境变量解决。

  1. [root@test-c2c-console01 application]# vim /etc/profile # 文件末尾添加python3路径
  2. PATH="/application/Python3/bin/:$PATH"
  3. [root@test-c2c-console01 application]# source /etc/profile # 更新环境变量
  4. [root@test-c2c-console01 application]# which python3
  5. /application/Python3/bin/python3
  6. [root@test-c2c-console01 application]# which python
  7. /usr/bin/python
  8. [root@test-c2c-console01 application]# python3 -V
  9. Python 3.5.2
  10. [root@test-c2c-console01 application]# python -V
  11. Python 2.6.6

启动系统默认的python

  1. [root@test-c2c-console01 application]# python
  2. Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
  3. [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>>

启动python3

  1. [root@test-c2c-console01 application]# python3
  2. Python 3.5.2 (default, Mar 1 2017, 09:02:01)
  3. [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>>

 

原文地址:https://www.cnblogs.com/yinshoucheng-golden/p/6482909.html