[linux] 非root安装Python2及其模块

需求

系统自带的python2版本太低,且没有想要的模块,非root用户无法安装。有些模块是python2写的,无法用python3,所以自己下载一个高版本的python2,可以自由下载模块。

实现

1.安装python2.7.15

最新的2.7.16可能不稳定,下了个2018年的修复版本。

wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
tar -xzf Python-2.7.15.tgz
cd Python-2.7.15
mkdir -p /your/other/python/path   #最好最好不要在源码下安装

./configure --prefix="/your/other/python/path"
make
make install

可以把python添加到环境变量或在bashrc中使用别名alias

2.安装setuptools

Python安装成功了,但要安装模块还需要pipsetuptools主要是为安装pip做准备的。

wget https://files.pythonhosted.org/packages/28/84/27df240f3f8f52511965979aad7c7b77606f8fe41d4c90f2449e02172bb1/setuptools-2.0.tar.gz

tar -xzvf setuptools-2.0.tar.gz
cd setuptools-2.0
/your/other/python/path/bin/python setup.py install #要用刚安装的python

3.安装pip

wget --no-check-certificate https://pypi.python.org/packages/41/27/9a8d24e1b55bd8c85e4d022da2922cb206f183e2d18fee4e320c9547e751/pip-8.1.1.tar.gz

tar -xzf pip-8.1.1.tar.gz
cd pip-8.1.1
/your/other/python/path/bin/python setup.py install ##要用刚安装的python

这时再看/your/other/python/path/bin/的python下已经安装了pip

4.安装模块

使用刚安装python下的pip来安装模块:
./pip install numpy
image.png
可以更新下pip:
image.png

Ref:https://www.csuldw.com/2016/05/06/2016-05-06-python-and-pip/
http://www.jouypub.com/2018/d12ca6e1eed82d77cb1fc149e4f13ce1/

原文地址:https://www.cnblogs.com/jessepeng/p/11610051.html