2021-05-12 14:32:11
CentOS 7.5下安裝Python 3.x與原有Python 2.x共存
Linux下預設系統自帶Python2.X的版本,這個版本被系統很多程式所依賴,所以不建議刪除,如果使用最新的Python3那麼我們知道編譯安裝原始碼包和系統預設包之間是沒有任何影響的,所以可以安裝Python3和Python2共存。
1、下載Linux平台的Python3.x的安裝包(本文測試安裝下載的是Python 3.7版本)
[linuxidc@localhost Linux公社 www.linuxidc.com]$ wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
2、解壓python3.6安裝包
[linuxidc@localhost Linux公社 www.linuxidc.com]$ tar xf Python-3.7.2.tgz
[linuxidc@localhost Linux公社 www.linuxidc.com]$ ll
總用量 22368
drwxrwxr-x 2 linuxidc linuxidc 30 2月 23 17:24 Linux公社
drwxr-xr-x 18 linuxidc linuxidc 4096 12月 24 11:41 Python-3.7.2
-rwxrw-rw- 1 linuxidc linuxidc 22897802 2月 23 19:04 Python-3.7.2.tgz
3、安裝Python3.7,預設安裝路徑為/usr/local
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
#安裝報錯,缺失依賴庫
[linuxidc@localhost Python-3.7.2]$ ./configure
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for --with-cxx-main=<compiler>... no
checking for g++... no
.......
如果您想要一個具有穩定優化的發布版本(PGO等),
請執行./configure --enable-optimizations
#如果有問題請安裝依賴庫
[linuxidc@localhost Python-3.7.2]$ yum install gcc
#安裝python3.7
[linuxidc@localhost Python-3.7.2]$ make && make install
#make && make install 報錯
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
#安裝zlib-devel
[root@localhost Python-3.7.2]# yum install zlib-devel
如果出現:
ModuleNotFoundError: No module named '_ctypes'
make: *** [install] 錯誤 1
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
#yum install libffi-devel -y 《-安裝這個就能解決
#重新安裝
[root@localhost Python-3.7.2]# make && make install
4、Python3.7已安裝成功,在/usr/local/bin下生成命令python3
[linuxidc@localhost linuxidc.com]$ ll /usr/local/bin/
總用量 27376
lrwxrwxrwx 1 root root 8 2月 23 19:25 2to3 -> 2to3-3.7
-rwxr-xr-x 1 root root 101 2月 23 19:25 2to3-3.7
lrwxrwxrwx 1 root root 8 2月 23 19:25 pydoc3 -> pydoc3.7
-rwxr-xr-x 1 root root 84 2月 23 19:25 pydoc3.7
lrwxrwxrwx 1 root root 9 2月 23 19:25 python3 -> python3.7
-rwxr-xr-x 2 root root 13979432 2月 23 19:23 python3.7
lrwxrwxrwx 1 root root 17 2月 23 19:25 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 13979432 2月 23 19:23 python3.7m
-rwxr-xr-x 1 root root 3097 2月 23 19:25 python3.7m-config
lrwxrwxrwx 1 root root 16 2月 23 19:25 python3-config -> python3.7-config
lrwxrwxrwx 1 root root 10 2月 23 19:25 pyvenv -> pyvenv-3.7
-rwxr-xr-x 1 root root 441 2月 23 19:25 pyvenv-3.7
lrwxrwxrwx 1 root root 6 2月 22 19:13 stream -> magick
5、使用Python3命令檢視版本
[linuxidc@localhost linuxidc.com]$ python3 -V
Python 3.7.2
[linuxidc@localhost linuxidc.com]$ python3
Python 3.7.2 (default, Feb 23 2019, 19:19:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
6、更換系統預設的Python版本
備份或刪除Python2.x
mv /usr/bin/python /usr/bin/python2.7
新建指向新版本的Python3.x和pip3的軟連線
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
如何利用pip將python模組安裝到指定的python版本中
問題
如電腦上同時裝了python2(2.7)和python3(3.7),當使用pip安裝時預設應安裝到python2中,pip3安裝時應安裝到python3中,但奇怪的是使用pip安裝時每次都定位到python3中,不知是啥原因,也不知如何將其重定向到python2中,索性手動指定pip到python2中
檢視pip版本
[root@localhost ipython]# pip -V
pip 19.0.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)
[root@localhost ipython]# pip2 -V
pip 19.0.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)
pip指定python版本安裝
安裝到python2.7版本中:sudo pip2 install 模組名 或 python2 -m pip install 模組名
安裝到python3.5版本中:sudo pip3 install 模組名 或 python3 -m pip install 模組名
修改yum相關設定
因yum的功能依賴於Python2.x,更改python預設版本後會導致 yum無法正常工作,所以要修改yum
vi /usr/bin/yum
修改第一行
#!/usr/bin/python2.7
相關文章