1. 程式人生 > >centos7安裝python3 以及tab補全功能 centos7安裝python3 以及tab補全功能

centos7安裝python3 以及tab補全功能 centos7安裝python3 以及tab補全功能

centos7安裝python3 以及tab補全功能

 

1.安裝python3

1.1下載python原始碼包

網址:https://www.python.org/downloads/release/python-362/

下載地址:https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz

1.1.1安裝python前的庫環境,非常重要

yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

1.2下載,解壓安裝原始碼包

 

複製程式碼
  1、./configure 是用來檢測你的安裝平臺的目標特徵的。比如它會檢測你是不是有CC或GCC,並不是需要CC或GCC,它是個shell指令碼。

  2、make 是用來編譯的,它從Makefile中讀取指令,然後編譯。

  3、make install是用來安裝的,它也從Makefile中讀取指令,安裝到指定的位置。
複製程式碼

 

 

 

wget https://www.python.org/ftp/python/3.4.7/Python-3.4.7.tar.xz
xz -d Python-3.4.7.tar.xz
tar -xf Python-3.4.7.tar
複製程式碼
#yum install gcc -y
./configure --prefix=/opt/python make && make install
---------------------
用python檢視預設安裝路徑
>>> import sys
>>> sys.path
['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6',
'/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
--------------------
python3.4預設沒新增path
在/etc/profile最後一行新增
export PATH=$PATH:/opt/python/bin
然後
source /etc/profile

#python快捷path就被改了,yum用的python2,麻煩
複製程式碼

1,安裝python前的庫環境,非常重要

yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

2,把壓縮包拽進來xshell裡

3,解壓  tar -xf Python-3.6.2.tgz

4,移動到opt  : mv ./Python-3.6.2.tgz

5,cd /opt

6, mkdir python36

7, ./configure --prefix=/opt/python36

8,  make && make install

 

 

1.2.3 牛逼的python Tab自動補全

下載setuptool 3.4以上已經預設安裝了easy_install-3.4

下載地址:
https://pypi.python.org/pypi/setuptools/36.2.7
下載命令:
wget https://pypi.python.org/packages/07/a0/11d3d76df54b9701c0f7bf23ea9b00c61c5e14eb7962bb29aed866a5844e/setuptools-36.2.7.zip#md5=b9e6c049617bac0f9e908a41ab4a29ac
解壓:
unzip setuptools-36.2.7.zip
編譯:
python3 setup.py build
python2 setup.py build
無誤後安裝:
python3 setup.py install
python2 setup.py install
沒什麼用

下載readline

#yum install -y readline-devel -y 
#pip install readline
複製程式碼
tab補全
#python startup file
import readline,rlcompleter
#tab completion
readline.parse_and_bind('tab:complete')
----------------------------
python歷史記錄
#history file
import os
history_file = os.path.join(os.environ["HOME"],".pythonhistory")
[[email protected]_local ~]# echo "export PYTHONSTARTUP='/root/.pythonstartup'" >> /root/.bashrc   
[[email protected]_local ~]# source /root/.bashrc  
[[email protected]_local ~]# cat /root/.pythonstartup   
import os  
import readline  
import rlcompleter  
import atexit  
  
  
#tab completion  
readline.parse_and_bind("tab: complete")  
  
  
#history file  
history_file = os.path.join(os.environ["HOME"],".pythonhistory")  
try:  
    readline.read_history_file(history_file)  
except IOError:  
    pass  
atexit.register(readline.write_history_file,history_file)  
  
  
del os,history_file,readline,rlcompleter  
[[email protected]_local ~]# python  
Python 2.7.3 (default, Jun  5 2013, 22:40:26)   
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2  
Type "help", "copyright", "credits" or "license" for more information.  
>>> import sys  
>>> sys.path<tab> #按tab鍵就可以自動補全了  
  
[[email protected]_local ~]# python  
>>>  
>>> sys.path #按上下方向鍵即可找到歷史命令 
整體程式碼

 加入python的啟動環境變數,永久生效自動補全

複製程式碼
只需要啟動時執行一條命令即可

import rlcompleter, readline
readline.parse_and_bind('tab:complete')
可以將上述的程式碼存於~/.pythonrc.py檔案中。

然後,在~/.bashrc中設定

export PYTHONSTARTUP=~/.pythonrc.py
這樣,每次啟動終端,bash就會設定PYTHONSTARTUP這個環境變數,然後,python啟動時就會使用這個變數指定的檔案中的初始化程式碼。

在python直譯器中,輸入一個變數的一部分,或者變數後面加句點,再按TAB鍵就可以看到自動補全的威力。

 

 

1.安裝python3

1.1下載python原始碼包

網址:https://www.python.org/downloads/release/python-362/

下載地址:https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz

1.1.1安裝python前的庫環境,非常重要

yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

1.2下載,解壓安裝原始碼包

 

複製程式碼
  1、./configure 是用來檢測你的安裝平臺的目標特徵的。比如它會檢測你是不是有CC或GCC,並不是需要CC或GCC,它是個shell指令碼。

  2、make 是用來編譯的,它從Makefile中讀取指令,然後編譯。

  3、make install是用來安裝的,它也從Makefile中讀取指令,安裝到指定的位置。
複製程式碼

 

 

 

wget https://www.python.org/ftp/python/3.4.7/Python-3.4.7.tar.xz
xz -d Python-3.4.7.tar.xz
tar -xf Python-3.4.7.tar
複製程式碼
#yum install gcc -y
./configure --prefix=/opt/python make && make install
---------------------
用python檢視預設安裝路徑
>>> import sys
>>> sys.path
['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6',
'/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
--------------------
python3.4預設沒新增path
在/etc/profile最後一行新增
export PATH=$PATH:/opt/python/bin
然後
source /etc/profile

#python快捷path就被改了,yum用的python2,麻煩
複製程式碼

1,安裝python前的庫環境,非常重要

yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

2,把壓縮包拽進來xshell裡

3,解壓  tar -xf Python-3.6.2.tgz

4,移動到opt  : mv ./Python-3.6.2.tgz

5,cd /opt

6, mkdir python36

7, ./configure --prefix=/opt/python36

8,  make && make install

 

 

1.2.3 牛逼的python Tab自動補全

下載setuptool 3.4以上已經預設安裝了easy_install-3.4

下載地址:
https://pypi.python.org/pypi/setuptools/36.2.7
下載命令:
wget https://pypi.python.org/packages/07/a0/11d3d76df54b9701c0f7bf23ea9b00c61c5e14eb7962bb29aed866a5844e/setuptools-36.2.7.zip#md5=b9e6c049617bac0f9e908a41ab4a29ac
解壓:
unzip setuptools-36.2.7.zip
編譯:
python3 setup.py build
python2 setup.py build
無誤後安裝:
python3 setup.py install
python2 setup.py install
沒什麼用

下載readline

#yum install -y readline-devel -y 
#pip install readline
複製程式碼
tab補全
#python startup file
import readline,rlcompleter
#tab completion
readline.parse_and_bind('tab:complete')
----------------------------
python歷史記錄
#history file
import os
history_file = os.path.join(os.environ["HOME"],".pythonhistory")
[[email protected]_local ~]# echo "export PYTHONSTARTUP='/root/.pythonstartup'" >> /root/.bashrc   
[[email protected]_local ~]# source /root/.bashrc  
[[email protected]_local ~]# cat /root/.pythonstartup   
import os  
import readline  
import rlcompleter  
import atexit  
  
  
#tab completion  
readline.parse_and_bind("tab: complete")  
  
  
#history file  
history_file = os.path.join(os.environ["HOME"],".pythonhistory")  
try:  
    readline.read_history_file(history_file)  
except IOError:  
    pass  
atexit.register(readline.write_history_file,history_file)  
  
  
del os,history_file,readline,rlcompleter  
[[email protected]_local ~]# python  
Python 2.7.3 (default, Jun  5 2013, 22:40:26)   
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2  
Type "help", "copyright", "credits" or "license" for more information.  
>>> import sys  
>>> sys.path<tab> #按tab鍵就可以自動補全了  
  
[[email protected]_local ~]# python  
>>>  
>>> sys.path #按上下方向鍵即可找到歷史命令 
整體程式碼

 加入python的啟動環境變數,永久生效自動補全

複製程式碼
只需要啟動時執行一條命令即可

import rlcompleter, readline
readline.parse_and_bind('tab:complete')
可以將上述的程式碼存於~/.pythonrc.py檔案中。

然後,在~/.bashrc中設定

export PYTHONSTARTUP=~/.pythonrc.py
這樣,每次啟動終端,bash就會設定PYTHONSTARTUP這個環境變數,然後,python啟動時就會使用這個變數指定的檔案中的初始化程式碼。

在python直譯器中,輸入一個變數的一部分,或者變數後面加句點,再按TAB鍵就可以看到自動補全的威力。