目錄

目錄

正文

前言:

python3應該是Python的趨勢所在,當然目前爭議也比較大,這篇隨筆的主要目的是記錄在centos6.7下搭建python3環境的過程

以及碰到的問題和解決過程。

另外,如果本機安裝了python2,儘量不要管他,使用python3執行python指令碼就好,因為可能有程式依賴目前的python2環境,

比如yum!!!!!

不要動現有的python2環境!

不要動現有的python2環境!

不要動現有的python2環境!

重要的使用說三遍!

安裝python3、6

一、檢視預設版本

檢視當前CentOS-6.7系統預設的python版本

[[email protected] ~]$ python -V
Python 2.6.6
[[email protected] ~]$
二、安裝依賴環境

此處需要使用root使用者進行安裝

[[email protected] hadoop]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
三、下載python3安裝包

 https://www.python.org

也可以從華中科技大學開源映象站裡面下載:http://mirrors.hust.edu.cn/

從官網下載,下載的版本是Python-3.6.4.tgz

四、安裝python3

個人習慣安裝在/usr/local/python3(具體安裝位置看個人喜好)

建立目錄:

[[email protected] bin]# mkdir -p /usr/local/python3
[[email protected] bin]# 

上傳並解壓安裝包:

[[email protected] soft]# ls
Python-3.6.4.tgz
[[email protected] soft]# tar -zxvf Python-3.6.4.tgz -C /usr/local/python3/
五、進入解壓後的目錄、新增配置
[[email protected] soft]# cd /usr/local/python3/
[[email protected] python3]# ls
Python-3.6.4
[[email protected] python3]# cd Python-3.6.4/
[[email protected] Python-3.6.4]# ./configure --prefix=/usr/local/python3
執行過程中報錯
[[email protected] Python-3.6.4]# ./configure --prefix=/usr/local/python3
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.6... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/python3/Python-3.6.4':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
[[email protected] Python-3.6.4]#

報錯原因是由於本機中缺少gcc編譯環境,2種解決方式

1、通過yum安裝gcc編譯環境:yum install -y gcc

2、本機沒有安裝yum功能,可下載gcc安裝包:https://gcc.gnu.org/

[[email protected] Python-3.6.4]# yum install -y gcc
[[email protected] Python-3.6.4]# yum install -y gcc


已安裝:
  gcc.x86_64 0:4.4.7-18.el6_9.2                                                                                 

作為依賴被安裝:
  cloog-ppl.x86_64 0:0.15.7-1.2.el6        cpp.x86_64 0:4.4.7-18.el6_9.2        mpfr.x86_64 0:2.4.1-6.el6       
  ppl.x86_64 0:0.10.2-11.el6              

作為依賴被升級:
  libgcc.x86_64 0:4.4.7-18.el6_9.2                       libgomp.x86_64 0:4.4.7-18.el6_9.2                      

完畢!
[[email protected] Python-3.6.4]#

重新執行命令 新增配置

[[email protected] Python-3.6.4]# ./configure --prefix=/usr/local/python3
[[email protected] Python-3.6.4]# ./configure --prefix=/usr/local/python3

checking for glibc _FORTIFY_SOURCE/memmove bug... no
checking for gcc ipa-pure-const bug... no
checking for stdatomic.h... no
checking for GCC >= 4.7 __atomic builtins... no
checking for ensurepip... upgrade
checking if the dirent structure of a d_type field... yes
checking for the Linux getrandom() syscall... no
checking for the getrandom() function... no
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile


If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations


[[email protected] Python-3.6.4]#
六、編譯原始碼
[[email protected] Python-3.6.4]# make

七、執行安裝
[[email protected] Python-3.6.4]# make install

八、將python3的目錄複製到/usr/bin/目錄下
[[email protected] /]# cp /usr/local/python3/ /usr/bin/
[[email protected] /]# 
九、建立軟連線
對/usr/bin目錄下的python3目錄重新命名為python364
[[email protected] bin]# mv python3 python364
建立軟連結
[[email protected] bin]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
可以ls -l檢視一下


十、配置環境變數
[[email protected] bin]# vi /etc/profile
#Python3.6.4
PATH=$PATH:$HOME/bin:/usr/bin/python3/bin
export PATH

修改完記得執行行下面的命令,讓上一步的修改生效:

[[email protected] bin]# source /etc/profile
十一、檢查驗證
[[email protected] bin]# python3 -V
Python 3.6.4
[[email protected] bin]# 

十二、安裝模組
[[email protected] bin]# pip3 install paramiko
bash: /usr/bin/pip3: 沒有那個檔案或目錄

報錯麼有這個命令

建立pip3的軟連線

[[email protected] bin]# ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

重新安裝模組

[[email protected] bin]# pip3 install paramiko
Collecting paramiko
  Could not find a version that satisfies the requirement paramiko (from versions: )
No matching distribution found for paramiko
You are using pip version 9.0.1, however version 9.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[[email protected] bin]#