Intro

实验室服务器限制在内网环境里连接,想要安装一些Python第三方包无法直接通过pip install安装。本次安装背景为两台操作系统与Python版本都近乎一致的机器(如有较大差别可参考不一致时的解决办法),一台可连外网,一台待离线安装。以安装PyHive包需要的库为例。

注:可以在Python交互式终端中用以下两条代码查出当前Python的具体环境信息:

1
2
import pip._internal
print(pip._internal.pep425tags.get_supported())

Online

pip&Python版本:

1
2
[cuper@iZe4h75o51zvd0Z ~]$ pip -V
pip 21.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

分别安装下列包:

1
2
3
4
pip download sasl
pip download thrift
pip download thrift-sasl
pip download PyHive

在当前文件夹会发现多出下列文件:

1
2
3
4
5
6
7
-rw-rw-r-- 1 cuper cuper    30409 Apr 29 20:19 sasl-0.2.1.tar.gz
-rw-rw-r-- 1 cuper cuper 10963 Apr 29 20:19 six-1.15.0-py2.py3-none-any.whl
-rw-rw-r-- 1 cuper cuper 59911 Apr 29 20:20 thrift-0.13.0.tar.gz
-rw-rw-r-- 1 cuper cuper 3944 Apr 29 20:21 thrift_sasl-0.4.2.tar.gz
-rw-rw-r-- 1 cuper cuper 41400 Apr 29 20:22 PyHive-0.6.3.tar.gz
-rw-rw-r-- 1 cuper cuper 227183 Apr 29 20:22 python_dateutil-2.8.1-py2.py3-none-any.whl
-rw-rw-r-- 1 cuper cuper 829220 Apr 29 20:22 future-0.18.2.tar.gz

然后拷到offline机器上即可。

Offline

首先创建一个本工程的虚拟环境并激活:

1
2
python3 -m venv clustering
. clustering/bin/activate

pip&Python版本:

1
2
(clustering) [lpj@cpu-node1 clustering]$ pip3 -V
pip 18.1 from /home/lpj/cluster/clustering/lib/python3.6/site-packages/pip (python 3.6)

创建一个pip_pack文件夹专门用来存放导过去的包,并把7个包拷进去:

1
2
cd clustering/
mkdir pip_pack

接着pip带参数离线安装:

1
2
3
4
pip3 install --no-index --find-links=. sasl
pip3 install --no-index --find-links=. thrift
pip3 install --no-index --find-links=. thrift-sasl
pip3 install --no-index --find-links=. PyHive

--find-links代表指定文件夹去找需要的包,.表示当前文件夹,然后pip在Collecting的时候会自动在指定文件夹寻找需要的包和相关的依赖:

1
2
3
4
5
6
7
8
Looking in links: .
Collecting thrift-sasl
Requirement already satisfied: thrift>=0.10.0 in /home/lpj/cluster/clustering/lib/python3.6/site-packages (from thrift-sasl) (0.13.0)
Requirement already satisfied: sasl>=0.2.1 in /home/lpj/cluster/clustering/lib/python3.6/site-packages (from thrift-sasl) (0.2.1)
Requirement already satisfied: six>=1.13.0 in /home/lpj/cluster/clustering/lib/python3.6/site-packages (from thrift-sasl) (1.15.0)
Installing collected packages: thrift-sasl
Running setup.py install for thrift-sasl ... done
Successfully installed thrift-sasl-0.4.2

出现Successfully installed即为安装成功。

在Python交互式命令行尝试导入包:

1
from pyhive import hive

没有报错即为安装成功,其他所缺的包安装同理。