centos python3安装环境准备

一. 准备 Python3 和 Python 虚拟环境

1.1 安装依赖包

$ yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release git

Yum 加速设置请参考

1.2 编译安装

$ wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
$ tar xvf Python-3.6.1.tar.xz  && cd Python-3.6.1
$ ./configure && make && make install
# 这里必须执行编译安装,否则在安装 Python 库依赖时会有麻烦...

1.3 建立 Python 虚拟环境

因为 CentOS 6/7 自带的是 Python2,而 Yum 等工具依赖原来的 Python,为了不扰乱原来的环境我们来使用 Python 虚拟环境

$ cd /opt
$ python3 -m venv py3
$ source /opt/py3/bin/activate
# 看到下面的提示符代表成功,以后运行 python程序 都要先运行以上 source 命令,以下所有命令均在该虚拟环境中运行
(py3) [root@localhost py3]

1.4 自动载入 Python 虚拟环境配置
此项仅为懒癌晚期的人员使用,防止运行 python程序 时忘记载入 Python 虚拟环境导致程序无法运行。使用autoenv

$ git clone git://github.com/kennethreitz/autoenv.git
$ echo 'source /opt/autoenv/activate.sh' >> ~/.bashrc
$ source ~/.bashrc

你可能感兴趣的:(centos python3安装环境准备)