1. 程式人生 > >如何在centos 7安裝python3.5

如何在centos 7安裝python3.5

1、下載python

我們以原始碼的方式進行安裝,進入/usr/local/src/,用wget下載原始碼:
[[email protected]_109_202_centos /]# cd /usr/local/src/
[[email protected]_109_202_centos src]# wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz

2、解壓縮

我們用tar命令把下載的包解壓縮,以下省略解壓所產生的資訊,如下:
[[email protected]_109_202_centos src]# ls -l
total 14484 -rw-r--r-- 1 root root 14808460 Sep 13 2015 Python-3.5.0.tar.xz [[email protected]_109_202_centos src]# tar xvJf Python-3.5.0.tar.xz ... ... [[email protected]_109_202_centos src]# ls -l total 14488 drwxrwxr-x 16 oldwang oldwang 4096 Sep 13 2015 Python-3.5.0 -rw-r--r-- 1 root root 14808460
Sep 13 2015 Python-3.5.0.tar.xz

3、編譯和安裝

進入Python-3.5.0資料夾,注: –prefix用於指定要安裝的目錄:
[[email protected]_109_202_centos src]# cd Python-3.5.0/
[[email protected]_109_202_centos Python-3.5.0]# ./configure --prefix=/usr/local/python-3.5
[[email protected]_109_202_centos Python-3.5.0]# make
[[email protected]
_109_202_centos Python-3.5.0]# make install

4、檢查python的版本

進入/usr/local/python-3.5/, 輸入python,我的提示資訊是這樣的:
[[email protected]_109_202_centos python-3.5]# python
Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
這可能跟系統的環境變數設定有關,輸入exit(),退出python。進入/usr/bin/目錄,看到有很多個python檔案:
>>> exit()
[[email protected]_109_202_centos python-3.5]# cd /usr/bin
[[email protected]_109_202_centos bin]# ls -l python*
lrwxrwxrwx 1 root root    7 Dec  5 13:05 python -> python2
lrwxrwxrwx 1 root root    9 Dec  5 13:05 python2 -> python2.7
-rwxr-xr-x 1 root root 7136 Sep 16 06:38 python2.7
如果我們用安裝的絕對路徑來執行python,就會看到python3.5版本:
[[email protected]_109_202_centos bin]# /usr/local/python-3.5/bin/python3
Python 3.5.0 (default, Feb  7 2017, 11:25:59) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
切換到/usr/local/python-3.5/bin/目錄也可以看到下面python檔案:
[[email protected]_109_202_centos bin]# ls -l
total 23960
lrwxrwxrwx 1 root root        8 Feb  7 11:28 2to3 -> 2to3-3.5
-rwxr-xr-x 1 root root      112 Feb  7 11:28 2to3-3.5
-rwxr-xr-x 1 root root      252 Feb  7 11:28 easy_install-3.5
lrwxrwxrwx 1 root root        7 Feb  7 11:28 idle3 -> idle3.5
-rwxr-xr-x 1 root root      110 Feb  7 11:28 idle3.5
-rwxr-xr-x 1 root root      224 Feb  7 11:28 pip3
-rwxr-xr-x 1 root root      224 Feb  7 11:28 pip3.5
lrwxrwxrwx 1 root root        8 Feb  7 11:28 pydoc3 -> pydoc3.5
-rwxr-xr-x 1 root root       95 Feb  7 11:28 pydoc3.5
lrwxrwxrwx 1 root root        9 Feb  7 11:28 python3 -> python3.5
lrwxrwxrwx 1 root root       16 Feb  7 11:28 python3-config -> python3.5-config
-rwxr-xr-x 2 root root 12230982 Feb  7 11:27 python3.5
lrwxrwxrwx 1 root root       17 Feb  7 11:28 python3.5-config -> python3.5m-config
-rwxr-xr-x 2 root root 12230982 Feb  7 11:27 python3.5m
-rwxr-xr-x 1 root root     3082 Feb  7 11:28 python3.5m-config
lrwxrwxrwx 1 root root       10 Feb  7 11:28 pyvenv -> pyvenv-3.5
-rwxr-xr-x 1 root root      247 Feb  7 11:28 pyvenv-3.5
把/usr/local/python-3.5/bin加到linux的環境變數中:
[[email protected]_109_202_centos ~]# echo $PATH
/root/java/jdk1.8.0_111/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected]_109_202_centos ~]# PATH="$PATH":/usr/local/python-3.5/bin/
[[email protected]_109_202_centos ~]# echo $PATH
/root/java/jdk1.8.0_111/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python-3.5/bin/
現在將python3.5的安裝路徑加到PATH環境變數,linux嚴格區分大小寫,所以PATH都是大寫的,設定變數之後,輸入python3,就會顯示python3.5的版本,如下所示:
[[email protected]_109_202_centos bin]# export PATH="$PATH:/usr/local/python-3.5/bin"
[[email protected]_109_202_centos bin]# echo $PATH
/root/java/jdk1.8.0_111/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python-3.5/bin
[[email protected]_109_202_centos local]# python3
Python 3.5.0 (default, Feb  7 2017, 13:42:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
現在我們進入/usr/bin/目錄,刪掉裡面的python檔案,然後再建立3.5的一個symbolic link,就可以讓系統執行我們的python3:
[[email protected]_109_202_centos bin]# ls -l python*
lrwxrwxrwx 1 root root    7 Dec  5 13:05 python -> python2
lrwxrwxrwx 1 root root    9 Dec  5 13:05 python2 -> python2.7
-rwxr-xr-x 1 root root 7136 Sep 16 06:38 python2.7
[[email protected]_109_202_centos local]# cd /usr/bin/
[[email protected]_109_202_centos bin]# rm -rf python
[[email protected]_109_202_centos bin]# ls -l python*
lrwxrwxrwx 1 root root    9 Dec  5 13:05 python2 -> python2.7
-rwxr-xr-x 1 root root 7136 Sep 16 06:38 python2.7
[[email protected]_109_202_centos bin]# ln -s /usr/local/python-3.5/bin/python3 ./python
[[email protected]_109_202_centos bin]# ls -l python*
lrwxrwxrwx 1 root root   33 Feb  7 14:24 python -> /usr/local/python-3.5/bin/python3
lrwxrwxrwx 1 root root    9 Dec  5 13:05 python2 -> python2.7
-rwxr-xr-x 1 root root 7136 Sep 16 06:38 python2.7
[[email protected]_109_202_centos bin]# python
Python 3.5.0 (default, Feb  7 2017, 13:42:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
此時我們可以看到,我們輸入python的時候,系統顯示的是python3.5版本,到現在基本就完成了。需要注意的是,上面關於PATH這種環境變數設定方式只對當前會話有效,退出linux登陸後,下次登陸我們輸入python3就失效了,因為這次我們的PATH裡沒有python3的安裝目錄,你可以檢驗一下,但輸入python依然會是python3.5版本,因為上面symbolic的原因。輸入python2,我的依然是2.7的版本。所以要想輸入python3依然有用,我建議修改~/.bash_profile檔案,這是一種針對使用者的設定方法:
[[email protected]_109_202_centos ~]# vi ~/.bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/python-3.5/bin

export PATH
在PATH=PATH:HOME/bin的後面加上:/usr/local/python-3.5/bin,/usr/local/python-3.5/bin這是我的python的安裝路徑的bin目錄,修改後如上面所示。注意:修改完成後還必須輸入source .bash_profile命令,否則修改不能生效。這時輸入如下命令,我們發現python3的安裝路徑已經加到PATH中,而且輸入python3也能找到python命令,退出登陸:
[[email protected]_109_202_centos ~]# clear
[[email protected]_109_202_centos ~]# echo $PATH
/root/java/jdk1.8.0_111/bin:/root/java/jdk1.8.0_111/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/python-3.5/bin
[[email protected]_109_202_centos ~]# python3
Python 3.5.0 (default, Feb  7 2017, 14:52:05) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[[email protected]_109_202_centos ~]# exit
我們再一次登陸主機:
[[email protected]_109_202_centos ~]# echo $PATH
/root/java/jdk1.8.0_111/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python-3.5/bin
[[email protected]_109_202_centos ~]# python3
Python 3.5.0 (default, Feb  7 2017, 14:52:05) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
這時發現我們上次設定的變數這次起了作用,系統能找到python3的安裝路徑了。至此,我們可以輸入python和python3來執行python了,python2則可以執行python2.7版本。這種PATH設定方式是針對使用者登陸環境設定檔案的,但我只有一個root使用者,所以我就這樣設定。哈哈!要想了解更多這方面的,可以參考博文【雜談】Linux系統修改環境變數PATH路徑—終結篇 ,裡面還有講到關於系統中的使用者工作環境。

總結:

本文主要講了python3.5的原始碼下載和安裝,以及關於一些環境變數的設定,主要在於將linux內建的python2.7版本換成我們安裝3.5版本,輸入python是連結到python3的,因為我們將python檔案作為python3的一個symbolic link.而直接輸入python3,系統能找到執行的python是因為我們把安裝路徑加到了使用者的PATH(全部大寫)中。本文還讓我們瞭解了基本的環境變數的設定。

相關推薦

Centos 7 安裝 Python3.5.2後yum不能正常使用的解決辦法

Centos 7後自帶Python2.7.5, 考慮到 Python3更新的內容比較多,於是安裝 Python3.5.2 來學習, 搭建了一個測試網站(hello.py) 後發現後臺還是以 Python 2.7.5 的環境執行 hello.py. 讓系統以 Python

CentOS 7 安裝Python3.5 及Django

1.首先用yum安裝下vim,因為CentOS 7可能根本沒自帶完整vim,經常出現輸入亂碼:yum -y install vim2.安裝開發工具組:yum groupinstall "Developm

如何在centos 7安裝python3.5

1、下載python 我們以原始碼的方式進行安裝,進入/usr/local/src/,用wget下載原始碼: [[email protected]_109_202_centos /]# cd /usr/local/src/ [[email&

CentOS 7 安裝MySQL 5.6遇到問題及解決方案

linux localhost not ces name 解決 info target case centos下安裝mysql真的沒有想象中那麽容易,在這裏我總結一下遇到的問題 1. ERROR 2002 (HY000): Can’t connect to loca

centos 7 安裝python3.6.1

python 3.6 安裝centos7 默認安裝了python2.7.5,當需要使用python3的時候,可以手動下載python源碼後編譯安裝.python 官網:www.python.org1.安裝python可能用到的依賴yum install openssl-devel bzip2-devel ex

centos 7 安裝 MySQL 5.6

計算機 mysqld rpm安裝 unity 文件中 change root server class   由於Centos7 默認數據庫是mariabd,所以通過rpm安裝MySQL需要卸載原有的Mariabd,再下載所有的依賴包比較麻煩且容易出錯。通過yum的方式安裝。

centos 7 安裝 MySQL 5.7

centos 7 yum 安裝 mysql5.7環境介紹服務器地址:192.168.99.122MySQL版本:MySQL 5.7mysql-community-server-5.7.19-1.el7.x86_64操作步驟#下載安裝yum源wget http://repo.mysql.com/mysql57-

朝花夕拾:linux CentOS 7 安裝mysql 5.7.13

linux centos 7 安裝mysql 5.7 1.安裝環境:[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core)Mysql版本號:mysql-5.7.13-linux-glibc2.5-

Centos 7安裝Python3.7

錯誤 新版本 源碼 ext python3 lock 功能 cto prefix 1.安裝編譯環境包(防止出現安裝錯誤) yum install gcc-c++ gcc make cmake zlib-devel bzip2-devel openssl-devel ncur

CentOS 7 - 安裝MySQL 5.7

tools -bash temp 找到 system lan mmu mysql etc CentOS 7的默認yum倉庫中並沒有MySQL5.7,我們需要手動添加,好在MySQL官方提供了倉庫的地址,所以我們能夠比較簡單地安裝MySQL。 本文我們將介紹CentOS 7下

【轉】新裝的CentOS 7安裝python3

url dev 來源 -m ces man root 目錄 安裝python https://blog.csdn.net/lovefengruoqing/article/details/79284573 centos7 自帶有 python,但是卻是 python2 版本

CentOS 7 安裝 MySQL 5.7.23

gre cmd 環境變量 under emp -i 阿裏 not -a 阿裏雲服務器 CentOS 7 安裝 MySQL 5.7.23 。 一、準備工作 1、MySQL 官網下載 MySQL 5.7.23 :mysql-5.7.23-linux-glibc2.12-x8

CentOS 7 安裝python3.7.1

https load 一個 compile all ctype zlib ESS 如果 安裝wget yum -y install wget   創建一個download目錄用於下載各種安裝包 mkdir download 切換到剛創建的download目錄

CentOS 7 安裝 MySQL 5.6.4 -- 通過二進位制包方式安裝

一、檢視系統已經安裝的mysql資料庫 1.檢視系統是否已經安裝了 mariadb [[email protected] ~]$ rpm -qa |grep mariadb mariadb-libs-5.5.52-1.el7.x86_64 2.檢視是否已經存在配置檔案 m

CentOS 7 安裝 python3 ,同時和python2 共存

環境介紹: Vultr VPS CentOS 7 X64 目標環境: Python3+Python2 步驟: 1). 備份Python2 python -V ## 檢視當前版本號 which python ## 檢視Python安裝目錄 cd /usr/bin

Centos 7 安裝Mysql-5.7

==========運維之路 環境如下 [[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [[email prote

linux系統centOS 7安裝 mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

#解除安裝系統自帶的Mariadb [[email protected] local]# rpm -qa|grep mariadb mariadb-libs-5.5.52-1.el7.x86_64 [[email protected] local]# rpm -e --nodep

CentOS 7 安裝 Python3.7

1.我們先看看現有的 python2在哪裡 [[email protected] usr]# whereis python python: /usr/bin/python3.7 /usr/bin/python /usr/bin/python2.7 /usr/lib

Centos 7安裝python3

純傻瓜式步驟,保證成功。 下面的操作,按照步驟來就可以了,不要在中途cd 到別的檔案目錄下,要想檢視效果可以用 lsj加上對應的目錄,不需要切換進去。 首先不管你當前在哪個目錄下,輸入以下命令。 [[email protected] /]# cd / [[em

centos 7 安裝python3

epel-release epel yum install epel-release webtatic rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm python yum ins