Linux服务器离线安装python第三方库

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 编写目的
  • 一、python离线安装pip
  • 二、python使用离线安装第三方库
  • 1.获取所需第三方库的whl文件
  • 方法一:不推荐
  • 方法二:pip download命令下载(推荐)
  • 2.安装whl文件
  • 总结

  • 编写目的

    本文基于以下情况为例,介绍如何在linux上离线安装python第三方库,方法适用于所有python版本 以及所有类型的第三方库。

    所需工具:另外一台装有相同python版本 能联网的Linux机器

    具体问题: 需要在现场环境linux服务器上运行python脚本,该脚本需要依赖Twisted等第三方库。linux服务器上的python版本是2.7.5,该服务器未联外网,且python2.7.5默认没有自带pip工具


    一、python离线安装pip

    linux 默认python2.7.5版本自带没有pip工具,安装方法(适用python2 python3):
    官网下载pip-20.3.1版本whl文件:

    ftp上传至linux服务器执行:

    python pip-20.3.1-py2.py3-none-any.whl/pip install pip-20.3.1-py2.py3-none-any.whl
    

    二、python使用离线安装第三方库

    1.获取所需第三方库的whl文件

    方法一:不推荐

    先使用联网的linux机器pip install 模块名命令安装需要的第三方库,目的是为了找到对应的离线whl文件具体版本,以及依赖模块的whl文件

    可以看到我们所需的whl文件有:

    Twisted-20.3.0-cp27-cp27mu-manylinux1_x86_64.whl
    incremental-21.3.0-py2.py3-none-any.whl
    ...
    six-1.16.0-py2.py3-none-any.whl
    

    接着去官网下载这些whl文件:https://pypi.org/
    Realease history找对应版本 →Download files→Built Distributions

    参考:https://blog.csdn.net/dietime1943/article/details/123791261

    方法二:pip download命令下载(推荐)

    直接使用pip download命令下载相关库安装包/以及依赖 到指定目录

    root@crms-10-10-178-157[/root]# mkdir whlfile
    root@crms-10-10-178-157[/root]# 
    root@crms-10-10-178-157[/root]# pip download Twisted -d whlfile 
    DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
    Collecting Twisted
      Using cached Twisted-20.3.0-cp27-cp27mu-manylinux1_x86_64.whl (3.2 MB)
    Collecting incremental>=16.10.1
      Using cached incremental-21.3.0-py2.py3-none-any.whl (15 kB)
    Collecting attrs>=19.2.0
      Using cached attrs-21.4.0-py2.py3-none-any.whl (60 kB)
    Collecting hyperlink>=17.1.1
      Using cached hyperlink-21.0.0-py2.py3-none-any.whl (74 kB)
    Collecting PyHamcrest!=1.10.0,>=1.9.0
      Using cached PyHamcrest-1.10.1.tar.gz (43 kB)
    Collecting constantly>=15.1
      Using cached constantly-15.1.0-py2.py3-none-any.whl (7.9 kB)
    Collecting Automat>=0.3.0
      Using cached Automat-20.2.0-py2.py3-none-any.whl (31 kB)
    Collecting zope.interface>=4.4.2
      Using cached zope.interface-5.4.0-cp27-cp27mu-manylinux2010_x86_64.whl (247 kB)
    Collecting idna>=2.5
      Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
    Collecting typing; python_version < "3.5"
      Using cached typing-3.10.0.0-py2-none-any.whl (26 kB)
    Collecting six
      Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
    Collecting setuptools
      Using cached setuptools-44.1.1-py2.py3-none-any.whl (583 kB)
    Saved ./whlfile/Twisted-20.3.0-cp27-cp27mu-manylinux1_x86_64.whl
    Saved ./whlfile/incremental-21.3.0-py2.py3-none-any.whl
    Saved ./whlfile/attrs-21.4.0-py2.py3-none-any.whl
    Saved ./whlfile/hyperlink-21.0.0-py2.py3-none-any.whl
    Saved ./whlfile/PyHamcrest-1.10.1.tar.gz
    Saved ./whlfile/constantly-15.1.0-py2.py3-none-any.whl
    Saved ./whlfile/Automat-20.2.0-py2.py3-none-any.whl
    Saved ./whlfile/zope.interface-5.4.0-cp27-cp27mu-manylinux2010_x86_64.whl
    Saved ./whlfile/idna-2.10-py2.py3-none-any.whl
    Saved ./whlfile/typing-3.10.0.0-py2-none-any.whl
    Saved ./whlfile/six-1.16.0-py2.py3-none-any.whl
    Saved ./whlfile/setuptools-44.1.1-py2.py3-none-any.whl
    Successfully downloaded Twisted incremental attrs hyperlink PyHamcrest constantly Automat zope.interface idna typing six setuptools
    root@crms-10-10-178-157[/root]# 
    root@crms-10-10-178-157[/root]# 
    root@crms-10-10-178-157[/root]# ls whlfile
    attrs-21.4.0-py2.py3-none-any.whl       idna-2.10-py2.py3-none-any.whl           six-1.16.0-py2.py3-none-any.whl
    Automat-20.2.0-py2.py3-none-any.whl     incremental-21.3.0-py2.py3-none-any.whl  Twisted-20.3.0-cp27-cp27mu-manylinux1_x86_64.whl
    constantly-15.1.0-py2.py3-none-any.whl  PyHamcrest-1.10.1.tar.gz                 typing-3.10.0.0-py2-none-any.whl
    hyperlink-21.0.0-py2.py3-none-any.whl   setuptools-44.1.1-py2.py3-none-any.whl   zope.interface-5.4.0-cp27-cp27mu-manylinux2010_x86_64.whl
    

    2.安装whl文件

    将whl文件ftp上传至服务器上离线安装,离线安装命令如下:

    pip install incremental-21.3.0-py2.py3-none-any.whl
    pip install attrs-21.4.0-py2.py3-none-any.whl
    pip install idna-2.10-py2.py3-none-any.whl
    pip install typing-3.10.0.0-py2-none-any.whl
    pip install hyperlink-21.0.0-py2.py3-none-any.whl
    pip install six-1.16.0-py2.py3-none-any.whl
    pip install PyHamcrest-1.10.1.tar.gz
    pip install constantly-15.1.0-py2.py3-none-any.whl
    pip install Automat-20.2.0-py2.py3-none-any.whl
    pip install zope.interface-5.4.0-cp27-cp27mu-manylinux2010_x86_64.whl
    pip install Twisted-20.3.0-cp27-cp27mu-manylinux1_x86_64.whl
    

    安装顺序依据Installing collected packages关键字:


    总结

    物联沃分享整理
    物联沃-IOTWORD物联网 » Linux服务器离线安装python第三方库

    发表评论