Redhat上离线/非root安装python库
文章目录
问题概述
需要在公司的服务器上安装python的pandas库,但是没有root权限,公司的服务器也无法访问外网(也是权限问题)。服务器上只有redhat自带的python2.6版本。
主要思路
VM创建一个与目标一样系统版本的虚拟机,使用同样版本的python,build相应的python包,然后复制到目标服务器上的用户文件里面,更新PYTHONPATH,使其可以被访问。
如果无法使用,则上传source压缩文件到目标服务器, 使用setup.py 安装(可能需要安装setuotools)。
Notes
Use external python package
- Copy single .py file
copy mydemo.py $python_dir/lib
- export PYTHONPATH
export PYTHONPATH="/home/lestat/Downloads/local/mypy/lib/python2.6/site-packages:${PYTHONPATH}"
export PYTHONPATH="/home/lestat/Downloads/local/mypy/lib64/python2.6/site-packages:${PYTHONPATH}"
- Append to sys.path
>>>import sys
>>>sys.path.append("/home/lestat/Downloads/mypy/lib/python2.6/site-packages/six-1.11.0-py2.6.egg")
RPM package
$ rpm --initdb --root /home/lestat/local --dbpath /home/lestat/local/lib/rpm
rpm --root /home/username/local --dbpath /home/username/local/lib/rpm \
--relocate /usr=/home/username/local --nodeps -ivh package.rpm
error : Unable to change root directory: Operation not permitted
Using rpm2cpio (Recommended)
$ wget http://mirrors.163.com/centos/6/os/x86_64/Packages/python-six-1.9.0-2.el6.noarch.rpm
Extract RPM file using rpm2cpio and cpio command
$rpm2cpio python-six-1.9.0-2.el6.noarch.rpm|cpio -idv
-i |Restore archive
-d |Create leading directories where need
-m|Retain previous file modification times when creating file
-v |Verbose i.e. display progress
Test python package using export PYTHONPATH
$ export PYTHONPATH="/home/lestat/Downloads/Packages/usr/lib/python2.6/site-packages:${PYTHONPATH}";
$ export PYTHONPATH="/home/lestat/Downloads/Packages/usr/lib64/python2.6/site-packages:${PYTHONPATH}"
$ python
>>>import six
>>>six.__path__
Build rpm pacakge from source
python setup.py bdist format=rpm
error -ba : unknow option
solution: yum install rpm-build
Linux release
bash-4.1$ cat /etc/redhat-release
bash-4.1$ Red Hat Entreprise Linux Server release 6.9 (Santiago)
Python release version
bash-4.1$ rpm -q python
bash-4.1$ python-2.6.6-66.e16_8.x86_64
Install gcc
bash-4.1$ gcc version 4.4.7 (Red Hat 4.4.7) (GCC)
Install python-devel
- python-devel-2.6.6-66.el6_8.x86_64.rpm
Install setuptools
bash-4.1$ wget http://peak.telecommunity.com/dist/ez_setup.py
In our case, use below version:
setuptools-0.6c11-py2.6.egg
Install Numpy
- numpy-1.6.1
Pandas
- pandas-0.8.0
pandas-0.8.0$ python setup.py build_ext --inplace --force
pandas-0.8.0$ python setup.py install --prefix=/home/username/local/
Install Dependencies
- python-dateutil
- dateutil-1.4.1
- pytz
- pytz-2010h-py2.6
- numexpr
- python-numexpr-1.4.1-3.puias6.x86_64.rpm
- bottleneck
- Bottleneck-0.8.0
- six
- six-1.11.0