无用的小知识 python pip 批量离线安装


最近遇到个问题,在外网被挡住的情况下,如何在内网(离线环境)以最省力的方式安装三方库及其依赖包。

正常情况的下,如果我们要在离线环境下安装有依赖的软件包会出现以下情况:

    pip install yyy                    安装yyy包

      -> error, requirement xxx       报错,缺失依赖包 xxx

        -> pip install xxx                     安装依赖包 xxx

        -> pip install yyy                     安装yyy包

        -> success                               成功

下面我以离线模式下安装scipy为例,安装scipy有个前置需求那就是numpy,如果在没装numpy的情况下安装scipy会出现报错的情况。

安装指令 

python -m pip install scipy-1.6.3-cp37-cp37m-win_amd64.whl

 

如此我需要先安装numpy在安装scipy才可以成功,但这毕竟太麻烦了,如果能在我安装scipy的时候它自动把numpy装上就更好了。

 

于是我在网上查找的很多方法终于pip官方文档中看到了个惊喜。

PIP
  --no-index

  Ignore package index (only looking at --find-links URLs instead).

  -f--find-links <url>

  If a URL or path to an html file, then parse for links to archives such as sdist (.tar.gz) or wheel (.whl) files. If a local path or file:// URL that’s a directory, then look for archives in the directory listing. Links to VCS project URLs are not supported.

 

简单翻译就是: 

  --no-index:  

        忽略软件包索引(仅查看--find-links URL)。

  --find-links <url>: 

        如果是指向html文件的URL或路径,则解析到诸如sdist(.tar.gz)或wheel(.whl)等文件的链接。如果目录是本地路径或file:// URL,则在目录列表中查找档案。不支持指向VCS项目URL的链接。

显而易见我们可以组合这两个command来实现当我安装 scipy 时他 --no-index 指令会阻止pip默认的索引位置(pypi.org)而--find-links会使pip在指定目录检索是否有 scipy 包以及其依赖包 numpy 并安装,这样我就不用敲两次pip install了。

说了这么多,直接亮代码。

 

pip install --no-index --find-links=指定的路径 scipy==1.6.3

 

当然不仅是单包也可以批量安装多个包,指令如下: requirement.txt

pip install --no-index --find-links=指定的路径 -r requirement.txt

结果如下:

妈妈再也不用担心我在跑回圈了!一条指令解放双手!

 

也是第一次写博客,如果本篇内容有不对的地方欢迎大家指出我会重新修改。

我们的征途是星辰大海

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM