CentOS python 2.x 升到 3.x 遇到的一些問題記錄

一些Python3的代碼比較耗時,想掛到VPS上跑,VPS 的 CentOS 默認只提供了Python2,所以需要升級到Python3。

查看系統和python版本:

查看Linux系統類型和版本

[root@** ~]# head -n 1 /etc/issue
CentOS release 6.7 (Final)
查看Python版本
[root@** ~]# python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

查完了記得退出Python環境。(我幹了件蠢事,在python下輸入 wget 結果提示我語法錯誤,半天才反應過來。)

或者直接用:

[root@** ~]# python -V
Python 2.6.6 

下載Python安裝包並安裝:

打開python官網ftp目錄 https://www.python.org/ftp/python/ ,挑一個的3.x版本。

下載安裝包

wget http://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

...
Saving to: “Python-3.5.2.tgz”
100%[======================================>] 20,566,643 11.5M/s in 1.7s
2016-10-08 01:22:56 (11.5 MB/s) - “Python-3.5.2.tgz” saved [20566643/20566643]

解壓安裝包

tar -zxvf Python-3.5.2.tgz

...
Python-3.5.2/Include/metagrammar.h
Python-3.5.2/Include/classobject.h
Python-3.5.2/Include/patchlevel.h
Python-3.5.2/Include/sysmodule.h
Python-3.5.2/Include/dynamic_annotations.h
Python-3.5.2/Include/tupleobject.h

進入解壓目錄

cd Python-3.5.2

創建安裝目錄

mkdir /usr/local/python3.5

編譯安裝第一步:配置

./configure --prefix=/usr/local/python3.5
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
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 `/root/Python-3.5.2':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details


報錯了。沒有C編輯器。裝個gcc吧:

安裝gcc

yum -y install gcc
...

Installed:
gcc.x86_64 0:4.4.7-17.el6

Dependency Installed:
cloog-ppl.x86_64 0:0.15.7-1.2.el6
cpp.x86_64 0:4.4.7-17.el6
glibc-devel.x86_64 0:2.12-1.192.el6
glibc-headers.x86_64 0:2.12-1.192.el6
kernel-headers.x86_64 0:2.6.32-642.6.1.el6
libgomp.x86_64 0:4.4.7-17.el6
mpfr.x86_64 0:2.4.1-6.el6
ppl.x86_64 0:0.10.2-11.el6

Dependency Updated:
glibc.x86_64 0:2.12-1.192.el6 glibc-common.x86_64 0:2.12-1.192.el6
libgcc.x86_64 0:4.4.7-17.el6 tzdata.noarch 0:2016g-2.el6

Complete!

一堆各種安裝包的下載和自動安裝之後,提示你安裝完成。

再試一下configure

./configure --prefix=/usr/local/python3.5

一堆checking冒出來,開始運行了

...
checking for unlinkat... yes
checking for unsetenv... yes
checking for utimensat... yes
checking for utimes... yes
checking for waitid... yes
checking for waitpid... yes
checking for wait3... yes
...
...
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
config.status: pyconfig.h is unchanged
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

檢查通過後,將生成用於編譯的MakeFile文件。此時,可以開始進行編譯了

編譯安裝第二步:編譯

make

...
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_dbm _gdbm _lzma
_sqlite3 _ssl _tkinter
readline zlib
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

running build_scripts
creating build/scripts-3.5
copying and adjusting /root/Python-3.5.2/Tools/scripts/pydoc3 -> build/scripts-3.5
copying and adjusting /root/Python-3.5.2/Tools/scripts/idle3 -> build/scripts-3.5
...
# Substitution happens here, as the completely-expanded BINDIR
# is not available in configure
sed -e "s,@EXENAME@,/usr/local/python3.5/bin/python3.5m," < ./Misc/python-config.in >python-config.py
# Replace makefile compat. variable references with shell script compat. ones; ->
sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config
...


時間可能長了點,花了幾分鐘,提示建立成功。

看到中間那一大片not found的模塊了麼,一開始我忽略了,導致後面出現了一些問題,我後面說。

至於兩行sed提示,參照其他博客的經驗,直接複製運行了一下這兩條文本處理指令:

sed -e "s,@EXENAME@,/usr/local/python3.5/bin/python3.5m," < ./Misc/python-config.in >python-config.py
sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config
重新make
make
同樣是successfully,一堆not found,只不過這次下面少了兩行sed提示,不知道和剛纔有什麼不同。不過既然沒提示其他問題,就接着往下裝吧。

編譯安裝第三步:安裝

make install

屏幕滾動,同樣需要跑幾分鐘。可能是遠程鏈接的國外VPS上安裝的原因,如果是本地機器可能會快一些吧。

...
rm -f /usr/local/python3.5/share/man/man1/python3.1
(cd /usr/local/python3.5/share/man/man1; ln -s python3.5.1 python3.1)
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--upgrade" ;; \
install|*) ensurepip="" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
Ignoring ensurepip failure: pip 8.1.1 requires SSL/TLS

不理會pip安裝的問題,先看一下Python3.5.2安裝成功了沒有

修改路徑

備份原有python命令執行文件

mv /usr/bin/python /usr/bin/pythonbak

創建新python軟連接

ln -s /usr/local/python3.5/bin/python3.5 /usr/bin/python
運行Python
python
Python 3.5.2 (default, Oct  8 2016, 02:05:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello World')
Hello World
>>>
提示3.5.2,用括號方式print成功,安裝完成!

其他問題:

接下來解決一些之前沒解決的問題。

CentOS升級Python導致yum無法使用

yun是在python2.6的支持下使用,查了一些博客,有的只是將python從2.6升到2.7,yum都會報錯。

yum

File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax

所以這裏我們需要改一下yum的配置文件,變更python支持路徑。

vi /usr/bin/yum

將第一行的python改成python2.6即可:

1. 點擊“i”進入插入修改模式;

2. “Esc”退出修改模式;

3. 冒號“:”打開控制檯,輸入"wq"回車,存盤退出。

#!/usr/bin/python2.6
import sys
try:
import yum
except ImportError:
print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
...

再試試yum

yum

Loaded plugins: fastestmirror
You need to give some command
Usage: yum [options] COMMAND

List of Commands:
...

OK,沒問題了,yum可以用了

Python升級時跳過的SSL模塊的問題

之前出現過的語句 

pip 8.1.1 requires SSL/TLS


我們用剛弄好的yum安裝一下ssl:

yum install openssl-devel

中間詢問是否下載的時候輸入“y”

... libselinux-utils x86_64 2.0.94-7.el6 base 82 k openssl x86_64 1.0.1e-48.el6_8.3 updates 1.5 MTransaction Summary================================================================================Install 7 Package(s)Upgrade 4 Package(s)Total download size: 4.4 MIs this ok [y/N]: y......Dependency Updated: krb5-libs.x86_64 0:1.10.3-57.el6 libselinux.x86_64 0:2.0.94-7.el6 libselinux-utils.x86_64 0:2.0.94-7.el6 openssl.x86_64 0:1.0.1e-48.el6_8.3Complete!

全是自動運行,Complete就完成了。

同理,也可以把其他之前提示no found的東西裝上,格式如下:

yum install sqlite sqlite-devel
yum install bzip2 bzip2-devel
yum install readline-devel

裝完之後重新配置安裝一下python

make clean && ./configure && make && sudo make install

PIP安裝(只做記錄)

關於pip這個東西折騰了半天,之前本以爲ssl正常了,可以弄pip了。結果發現提示[忘了截圖了]大概意思是缺少模塊之類的。參照網上經驗下載了個ez_setup.py(wget https://bootstrap.pypa.io/ez_setup.py)然後用2.6版本的python執行了一下,再輸入pip還是提示有問題。我是在這之後才重新配置安裝python的。

這次重裝python後pip和setuptools雖然都提示安裝成功了,但是pip還是用不了。其中一句錯誤提示是需要pip7.1

pkg_resources.VersionConflict: (pip 8.1.1 (/usr/local/python3.5/lib/python3.5/site-packages), Requirement.parse('pip==7.1.0'))

沒怎麼細看,直接去找7.1 https://pypi.python.org/pypi/pip/7.1.0 ,下載裝上

wget https://pypi.python.org/packages/7e/71/3c6ece07a9a885650aa6607b0ebfdf6fc9a3ef8691c44b5e724e4eee7bf2/pip-7.1.0.tar.gz#md5=d935ee9146074b1d3f26c5f0acfd120e
tar -xzvf pip-7.1.0.tar.gz
cd pip-7.1.0
python setup.py install
然後再來一句pip,終於能用了

pip

Usage:
pip <command> [options]

Commands:
install Install packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
search Search PyPI for packages.
wheel Build wheels from your requirements.

再查一下pip版本

pip -V

pip 7.1.0 from /usr/local/python3.5/lib/python3.5/site-packages/pip-7.1.0-py3.5.egg (python 3.5)

貌似是之前的版本裝高了,能用就好。

裝個requests試一下:

pip install requests

You are using pip version 7.1.0, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting requests
Using cached requests-2.11.1-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.11.1

python

Python 3.5.2 (default, Oct  9 2016, 01:20:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.get('http://www.baidu.com').text
'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible ...

沒問題嘍~先用用看

=====

有的時候pip install requests提示已安裝,但是運行python3 卻提示 ImportError: No module named 'requests',的解決方法如下:

直接安裝到python3的目錄中

pip install --target='/usr/lib/python3.5/' requests
或安裝到要執行的py文件所在目錄

pip install -t 文件運行目錄 requests

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章