一些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
安装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