Linux安装Python3

CentOS7默认安装的是python2.7,因此需要手动安装Python3.7。

  • 下载:
# yum -y install gcc zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel readline readline-devel openssl openssl-devel openssl-static xz lzma xz-devel sqlite sqlite-devel gdbm gdbm-devel tk tk-devel libffi libffi-devel

# cd /software

# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
  • 解压:
# tar zxf Python-3.7.3.tgz
  • 安装:
# cd Python-3.7.3

# ./configure --prefix=/usr/python --enable-shared CFLAGS=-fPIC

# make && make install
  • 创建软链接:
# ln -s /usr/python/bin/python3 /usr/bin/python3

# ln -s /usr/python/bin/pip3 /usr/bin/pip3
  • 添加lib:
# vim /etc/ld.so.conf.d/python3.conf
/usr/python/lib

# ldconfig
  • 查看版本:
# python3 --version
Python 3.7.3
  • 安装ipython:
# pip3 install ipython

# vim ~/.bashrc
alias ipython='python3 -m IPython'

# source !$

后面运行ipython进入python命令行。


此外,如果python3的软链接直接指向python,就像这样:

# mv /usr/bin/python /usr/bin/python.bak

# ln -s /usr/python/bin/python3 /usr/bin/python

后续在执行yum命令时会报错,这是因为/usr/bin/yum使用的是python2,python3版本会报错:

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

这样的话还是像上面那样做软链接就好了。


你可能感兴趣的:(#,Python小知识)