白酒清茶无别事
我在等风也等你

Centos 编译安装 Python3.7(也适用于其他版本Python)

首先安装开发工具 和其他必需包

yum groupinstall -y "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel

下载python3.7 并且配置参数
cd /root
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
tar zxf Python-3.7.0.tgz
cd Python-3.7.0
./configure --prefix=/usr/local/python3.7 //配置安装路径
make //先编译下,没报错直接下一步编译安装
make install //编译安装

但是报错了,错误代码如下
Traceback (most recent call last):
File "/root/Python-3.7.0/Lib/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/root/Python-3.7.0/Lib/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/root/Python-3.7.0/Lib/ensurepip/__main__.py", line 5, in
sys.exit(ensurepip._main())
File "/root/Python-3.7.0/Lib/ensurepip/__init__.py", line 204, in _main
default_pip=args.default_pip,
File "/root/Python-3.7.0/Lib/ensurepip/__init__.py", line 117, in _bootstrap
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
File "/root/Python-3.7.0/Lib/ensurepip/__init__.py", line 27, in _run_pip
import pip._internal
File "/tmp/tmp5rlm41e_/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/__init__.py",
line 42, in
File "/tmp/tmp5rlm41e_/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/cmdoptions.py"
, line 16, in
File "/tmp/tmp5rlm41e_/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", lin
e 25, in
File "/tmp/tmp5rlm41e_/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py",
line 39, in
File "/tmp/tmp5rlm41e_/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/utils/glibc.py
", line 3, in
File "/root/Python-3.7.0/Lib/ctypes/__init__.py", line 7, in
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
make: *** [install] Error 1

然后Google了下,解决方法如下
Install -- yum install libffi-devel
./configure --enable-optimizations --prefix=/usr/local/python3.7 //python3.7的解压目录执行
make altinstall

好了,就这样,编译安装好了。
那新的问题又来了,我不能每次都cd /usr/local/python3.7/bin 下执行python3.7 是吧
我们也想像执行python一样便捷
那我们就来修改下PATH
echo $PATH //先输入下原来的PATH值,然后复制下,在PATH的末尾追加python3.7的目录

我们复制上面输出的PATH值
vim vim /etc/profile 

然后按键盘i进入编辑模式,在文本的末尾追加如下代码
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python3.7/bin/
#末尾的 /usr/local/python3.7/bin/ 为python3.7 的安装目录
export PATH

好了,这样系统就共存python2 和python3了,也很方便使用

赞(0) 打赏
未经允许不得转载:十一同学 » Centos 编译安装 Python3.7(也适用于其他版本Python)
分享到: 更多 (0)

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    niubi

    xiaobubutadie10个月前 (08-07)回复

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏