Python离线安装第三方库详细操作流程

 更新时间:2023年11月13日 10:19:09   作者:胡八一  
在使用Python开发过程中,我们经常需要使用各种第三方库来扩展Python的功能,这篇文章主要给大家介绍了关于Python离线安装第三方库的相关资料,需要的朋友可以参考下

1、场景介绍

在一些服务器上,我们搭建完Python环境之后,因为服务器的网络限制原因,不能直接通过pip命令下载安装Python的依赖包。
因此,我们需要在可以正常上网的服务器上下载好所需的依赖包文件,然后拷贝到目标服务器,通过pip在目标服务器上进行安装。

2、准备requirements.txt文件

requirements.txt文件罗列的是依赖包列表,表示你要下载的依赖包以及对应的版本。
该文件可以通过两种方式创建,分别是pip命令创建和手工创建。

jupyter==1.0.0
notebook==6.1.0

(1)pip命令创建requirements.txt,可以把当前服务器的Python依赖包生成列表到requirements.txt文件中。
【pip list】
【pip freeze >requirements.txt】

(2)手工创建requirements.txt,直接新建一个requirements.txt文件,然后按照格式填写所需依赖包以及对应版本号。

3、下载依赖包

注意:该步骤需要在可以正常上网的服务器上下载。下载的依赖包文件如图2所示。

pip download -r requirements.txt -d packages/ -i https://pypi.tuna.tsinghua.edu.cn/simple

其中:(packages/是要生成的文件夹,存放下载的依赖包文件;https://pypi.tuna.tsinghua.edu.cn/simple是镜像源地址)。

4、安装依赖包

准备好依赖包之后,就可以在无法使用pip命令下载安装依赖包的目标服务器上进行安装了。

使用命令之前,需要把packsges文件夹、requirements.txt文件拷贝到目标服务器,下面以requests依赖包为例。

pip install --no-index --find-links=./packages -r ./requirements.txt

(其中:./packsges是依赖包的所在路径;./requirements.txt是依赖包的列表路径)。

D:\zjj\pip>pip list
Package                           Version
--------------------------------- ---------
anyio                             3.7.1
argon2-cffi                       21.3.0
argon2-cffi-bindings              21.2.0
arrow                             1.2.3
asttokens                         2.2.1
async-lru                         2.0.4
attrs                             23.1.0
Babel                             2.12.1
backcall                          0.2.0
beautifulsoup4                    4.12.2
bleach                            6.0.0
certifi                           2023.7.22
cffi                              1.15.1
charset-normalizer                3.2.0
colorama                          0.4.6
comm                              0.1.3
debugpy                           1.6.7
decorator                         5.1.1
defusedxml                        0.7.1
distlib                           0.3.7
exceptiongroup                    1.1.2
executing                         1.2.0
fastjsonschema                    2.18.0
filelock                          3.12.2
fqdn                              1.5.1
idna                              3.4
importlib-metadata                6.8.0
importlib-resources               6.0.0
ipykernel                         6.25.0
ipython                           8.12.2
ipython-genutils                  0.2.0
ipywidgets                        8.0.7
isoduration                       20.11.0
jedi                              0.19.0
Jinja2                            3.1.2
json5                             0.9.14
jsonpointer                       2.4
jsonschema                        4.18.4
jsonschema-specifications         2023.7.1
jupyter_client                    8.3.0
jupyter-console                   6.6.3
jupyter-contrib-core              0.4.2
jupyter_core                      5.3.1
jupyter-events                    0.6.3
jupyter-highlight-selected-word   0.2.0
jupyter-lsp                       2.2.0
jupyter-nbextensions-configurator 0.6.3
jupyter_server                    2.7.0
jupyter_server_terminals          0.4.4
jupyterlab                        4.0.3
jupyterlab-pygments               0.2.2
jupyterlab_server                 2.24.0
jupyterlab-widgets                3.0.8
lxml                              4.9.3
MarkupSafe                        2.1.3
matplotlib-inline                 0.1.6
mistune                           3.0.1
nbclient                          0.8.0
nbconvert                         7.7.3
nbformat                          5.9.1
nest-asyncio                      1.5.7
notebook                          7.0.0
notebook_shim                     0.2.3
overrides                         7.3.1
packaging                         23.1
pandocfilters                     1.5.0
parso                             0.8.3
pickleshare                       0.7.5
pip                               23.2.1
pipenv                            2023.7.23
pkgutil_resolve_name              1.3.10
platformdirs                      3.10.0
prometheus-client                 0.17.1
prompt-toolkit                    3.0.39
psutil                            5.9.5
pure-eval                         0.2.2
pycparser                         2.21
Pygments                          2.15.1
python-dateutil                   2.8.2
python-json-logger                2.0.7
pytz                              2023.3
pywin32                           306
pywinpty                          2.0.11
PyYAML                            6.0.1
pyzmq                             25.1.0
qtconsole                         5.4.3
QtPy                              2.3.1
referencing                       0.30.0
requests                          2.31.0
rfc3339-validator                 0.1.4
rfc3986-validator                 0.1.1
rpds-py                           0.9.2
Send2Trash                        1.8.2
setuptools                        68.0.0
six                               1.16.0
sniffio                           1.3.0
soupsieve                         2.4.1
stack-data                        0.6.2
terminado                         0.17.1
tinycss2                          1.2.1
tomli                             2.0.1
tornado                           6.3.2
traitlets                         5.9.0
typing_extensions                 4.7.1
uri-template                      1.3.0
urllib3                           2.0.4
virtualenv                        20.24.2
virtualenv-clone                  0.5.7
wcwidth                           0.2.6
webcolors                         1.13
webencodings                      0.5.1
websocket-client                  1.6.1
widgetsnbextension                4.0.8
zipp                              3.16.2

D:\zjj\pip>pip install --no-index --find-links=./packages -r ./requirements.txt
Looking in links: ./packages
Processing d:\zjj\pip\packages\requests-2.28.2-py3-none-any.whl (from -r ./requirements.txt (line 1))
Requirement already satisfied: charset-normalizer<4,>=2 in d:\environment\python\python3.8.10\lib\site-packages (from requests==2.28.2->-r ./requirements.txt (line 1)) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in d:\environment\python\python3.8.10\lib\site-packages (from requests==2.28.2->-r ./requirements.txt (line 1)) (3.4)
Processing d:\zjj\pip\packages\urllib3-1.26.16-py2.py3-none-any.whl (from requests==2.28.2->-r ./requirements.txt (line 1))
Requirement already satisfied: certifi>=2017.4.17 in d:\environment\python\python3.8.10\lib\site-packages (from requests==2.28.2->-r ./requirements.txt (line 1)) (2023.7.22)
Installing collected packages: urllib3, requests
  Attempting uninstall: urllib3
    Found existing installation: urllib3 2.0.4
    Uninstalling urllib3-2.0.4:
      Successfully uninstalled urllib3-2.0.4
  Attempting uninstall: requests
    Found existing installation: requests 2.31.0
    Uninstalling requests-2.31.0:
      Successfully uninstalled requests-2.31.0
Successfully installed requests-2.28.2 urllib3-1.26.16

D:\zjj\pip>pip list
Package                           Version
--------------------------------- ---------
anyio                             3.7.1
argon2-cffi                       21.3.0
argon2-cffi-bindings              21.2.0
arrow                             1.2.3
asttokens                         2.2.1
async-lru                         2.0.4
attrs                             23.1.0
Babel                             2.12.1
backcall                          0.2.0
beautifulsoup4                    4.12.2
bleach                            6.0.0
certifi                           2023.7.22
cffi                              1.15.1
charset-normalizer                3.2.0
colorama                          0.4.6
comm                              0.1.3
debugpy                           1.6.7
decorator                         5.1.1
defusedxml                        0.7.1
distlib                           0.3.7
exceptiongroup                    1.1.2
executing                         1.2.0
fastjsonschema                    2.18.0
filelock                          3.12.2
fqdn                              1.5.1
idna                              3.4
importlib-metadata                6.8.0
importlib-resources               6.0.0
ipykernel                         6.25.0
ipython                           8.12.2
ipython-genutils                  0.2.0
ipywidgets                        8.0.7
isoduration                       20.11.0
jedi                              0.19.0
Jinja2                            3.1.2
json5                             0.9.14
jsonpointer                       2.4
jsonschema                        4.18.4
jsonschema-specifications         2023.7.1
jupyter_client                    8.3.0
jupyter-console                   6.6.3
jupyter-contrib-core              0.4.2
jupyter_core                      5.3.1
jupyter-events                    0.6.3
jupyter-highlight-selected-word   0.2.0
jupyter-lsp                       2.2.0
jupyter-nbextensions-configurator 0.6.3
jupyter_server                    2.7.0
jupyter_server_terminals          0.4.4
jupyterlab                        4.0.3
jupyterlab-pygments               0.2.2
jupyterlab_server                 2.24.0
jupyterlab-widgets                3.0.8
lxml                              4.9.3
MarkupSafe                        2.1.3
matplotlib-inline                 0.1.6
mistune                           3.0.1
nbclient                          0.8.0
nbconvert                         7.7.3
nbformat                          5.9.1
nest-asyncio                      1.5.7
notebook                          7.0.0
notebook_shim                     0.2.3
overrides                         7.3.1
packaging                         23.1
pandocfilters                     1.5.0
parso                             0.8.3
pickleshare                       0.7.5
pip                               23.2.1
pipenv                            2023.7.23
pkgutil_resolve_name              1.3.10
platformdirs                      3.10.0
prometheus-client                 0.17.1
prompt-toolkit                    3.0.39
psutil                            5.9.5
pure-eval                         0.2.2
pycparser                         2.21
Pygments                          2.15.1
python-dateutil                   2.8.2
python-json-logger                2.0.7
pytz                              2023.3
pywin32                           306
pywinpty                          2.0.11
PyYAML                            6.0.1
pyzmq                             25.1.0
qtconsole                         5.4.3
QtPy                              2.3.1
referencing                       0.30.0
requests                          2.28.2
rfc3339-validator                 0.1.4
rfc3986-validator                 0.1.1
rpds-py                           0.9.2
Send2Trash                        1.8.2
setuptools                        68.0.0
six                               1.16.0
sniffio                           1.3.0
soupsieve                         2.4.1
stack-data                        0.6.2
terminado                         0.17.1
tinycss2                          1.2.1
tomli                             2.0.1
tornado                           6.3.2
traitlets                         5.9.0
typing_extensions                 4.7.1
uri-template                      1.3.0
urllib3                           1.26.16
virtualenv                        20.24.2
virtualenv-clone                  0.5.7
wcwidth                           0.2.6
webcolors                         1.13
webencodings                      0.5.1
websocket-client                  1.6.1
widgetsnbextension                4.0.8
zipp                              3.16.2

D:\zjj\pip>

总结

到此这篇关于Python离线安装第三方库的文章就介绍到这了,更多相关Python离线安装第三方库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python 公共方法汇总解析

    python 公共方法汇总解析

    这篇文章主要介绍了python 公共方法汇总解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • python中threading和queue库实现多线程编程

    python中threading和queue库实现多线程编程

    这篇文章主要介绍了python中threading和queue库实现多线程编程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python 函数装饰器应用教程

    Python 函数装饰器应用教程

    函数装饰器是Python提供的一种增强函数功能的标记函数,本文将带大家深入学习一下Python 函数装饰器,感兴趣的同学跟随小编一起学习吧
    2021-12-12
  • pytorch中fuse_modules源码解读

    pytorch中fuse_modules源码解读

    这篇文章主要介绍了pytorch中fuse_modules,fuse_known_modules将给定的模块列表mod_list中的一些常见模块进行融合,返回融合后的模块列表,本文通过实例代码详细讲解,需要的朋友可以参考下
    2023-05-05
  • Python hug库构建快速可扩展的Web API框架使用详解

    Python hug库构建快速可扩展的Web API框架使用详解

    这篇文章主要介绍了Python hug库构建快速可扩展的Web API框架使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-02-02
  • Python使用cx_Oracle调用Oracle存储过程的方法示例

    Python使用cx_Oracle调用Oracle存储过程的方法示例

    这篇文章主要介绍了Python使用cx_Oracle调用Oracle存储过程的方法,结合具体实例分析了Python中通过cx_Oracle调用PL/SQL的具体步骤与相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • Python文件及目录处理的方法

    Python文件及目录处理的方法

    这篇文章主要介绍了Python文件及目录处理的方法,Python可以用于处理文本文件和二进制文件,比如创建文件、读写文件等操作。本文介绍Python处理目录以及文件的相关资料,需要的朋友可以参考一下
    2021-12-12
  • 如何使用python爬虫爬取要登陆的网站

    如何使用python爬虫爬取要登陆的网站

    这篇文章主要介绍了如何使用python爬虫爬取要登陆的网站,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • python读取测试数据的多种方式

    python读取测试数据的多种方式

    本文主要介绍了python读取测试数据的多种方式,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 运筹学-Python实现图论与最短距离

    运筹学-Python实现图论与最短距离

    需要求解任意两个节点之间的最短距离,使用 Floyd 算法,只要求解单源最短路径问题,有负权边时使用 Bellman-Ford 算法,没有负权边时使用 Dijkstra 算法,本节我们只讨论Dijkstra 算法,需要的朋友可以参考一下
    2022-01-01

最新评论