如何使用Python的pip下载并保存包的压缩文件?
如果我想使用 pip 命令下载包(及其依赖项),但保留下载的所有压缩文件(例如 django-socialregistration.tar .gz) - 有办法做到这一点吗?
我尝试过各种命令行选项,但它似乎总是解压并删除 zip 文件 - 或者它获取 zip 文件,但仅获取原始包,而不是原始包依赖关系。
If I want to use the pip
command to download a package (and its dependencies), but keep all of the zipped files that get downloaded (say, django-socialregistration.tar.gz) - is there a way to do that?
I've tried various command-line options, but it always seems to unpack and delete the zipfile - or it gets the zipfile, but only for the original package, not the dependencies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
pip install --download
已弃用。从版本 8.0.0 开始,您应该使用pip download
命令:pip install --download
is deprecated. Starting from version 8.0.0 you should usepip download
command:--download-cache
选项应该执行您想要的操作:但是,当我对此进行测试时,主包已下载、保存并安装正常,但依赖项是以其完整 url 路径保存的名称 - 有点烦人,但所有
tar.gz
文件都在那里。--download
选项下载主包及其依赖项,但不安装其中任何一个。 不下载依赖项。)(注意,在版本 1.1 之前,
--download
选项 pip 文档概述了使用--download
进行 快速&本地安装。The
--download-cache
option should do what you want:However, when I tested this, the main package downloaded, saved and installed ok, but the the dependencies were saved with their full url path as the name - a bit annoying, but all the
tar.gz
files were there.The
--download
option downloads the main package and its dependencies and does not install any of them. (Note that prior to version 1.1 the--download
option did not download dependencies.)The
pip
documentation outlines using--download
for fast & local installs.我总是这样做来下载软件包:
pip install --download /path/to/download/to_packagename
或
pip install --download=/path/to/packages/downloaded -r requests .txt
当我想安装我刚刚下载的所有库时,我这样做:
pip install --no-index --find-links="/path/to/downloaded/dependencies"包名
或
pip install --no-index --find-links="/path/to/downloaded/packages" -rrequirements.txt
更新
另外,要获取一个系统上安装的所有软件包,您可以将它们全部导出到
requirement.txt
中,以便将它们安装到另一个系统上,我们这样做:pip freeze > requirement.txt
然后,可以像上面一样使用
requirement.txt
进行下载,或者执行此操作从requirement.txt
安装它们:pip install -r request.txt
参考:pip 安装程序
I always do this to download the packages:
pip install --download /path/to/download/to_packagename
OR
pip install --download=/path/to/packages/downloaded -r requirements.txt
And when I want to install all of those libraries I just downloaded, I do this:
pip install --no-index --find-links="/path/to/downloaded/dependencies" packagename
OR
pip install --no-index --find-links="/path/to/downloaded/packages" -r requirements.txt
Update
Also, to get all the packages installed on one system, you can export them all to
requirement.txt
that will be used to intall them on another system, we do this:pip freeze > requirement.txt
Then, the
requirement.txt
can be used as above for download, or do this to install them fromrequirement.txt
:pip install -r requirement.txt
REFERENCE: pip installer
pipwheel
是您应该考虑的另一个选项:它将下载包及其依赖项到一个目录(默认为当前工作目录),但它执行将任何源包转换为轮子的附加步骤。
它可以方便地支持需求文件:
如果您只需要特别请求的包,请添加
--no-deps
参数:pip wheel
is another option you should consider:It will download packages and their dependencies to a directory (current working directory by default), but it performs the additional step of converting any source packages to wheels.
It conveniently supports requirements files:
Add the
--no-deps
argument if you only want the specifically requested packages:使用
pip download
下载所有包,包括依赖项使用
pip install --no-index --find-links 。
安装所有包(包括依赖项)。它从
CWD
获取所有文件。它不会下载任何东西
Use
pip download <package1 package2 package n>
to download all the packages including dependenciesUse
pip install --no-index --find-links . <package1 package2 package n>
to install all the packages including dependencies.It gets all the files from
CWD
.It will not download anything
在版本 7.1.2 中,pip 使用以下内容下载包的轮子(如果可用):
以下内容下载源发行版:
如果 pip 知道它们,它们也会下载依赖项(例如,如果
pip show package
列出了它们)。更新
正如Anton Khodak所述,
pip download
命令从版本 8 开始是首选。在上面的示例中,这意味着需要使用选项-d
给出/path/to/downloaded/file
,因此替换install与
下载
有效。In version 7.1.2 pip downloads the wheel of a package (if available) with the following:
The following downloads a source distribution:
These download the dependencies as well, if pip is aware of them (e.g., if
pip show package
lists them).Update
As noted by Anton Khodak,
pip download
command is preferred since version 8. In the above examples this means that/path/to/downloaded/file
needs to be given with option-d
, so replacinginstall
withdownload
works.离线安装 python 包
对于 windows 用户:
下载到文件中
打开您的 cmd 并按照以下步骤操作:
cd <*您要保存它的文件路径*>
pip download <*包名称*> ;
包和依赖项将被下载到当前工作目录中。
要从当前工作目录安装:
将下载的文件夹设置为 cwd,然后按照以下步骤操作:
pip install <*下载为 .whl*> 的包名称--no-index --find-links <*下载文件的文件位置*>
这将在该位置搜索依赖项。
installing python packages offline
For windows users:
To download into a file
open your cmd and folow this:
cd <*the file-path where you want to save it*>
pip download <*package name*>
the package and the dependencies will be downloaded in the current working directory.
To install from the current working directory:
set your folder where you downloaded as the cwd then follow these:
pip install <*the package name which is downloded as .whl*> --no-index --find-links <*the file locaation where the files are downloaded*>
this will search for dependencies in that location.
本线程中提到的所有答案都假设软件包将在与必须安装的目标操作系统相同的操作系统配置上下载。
根据我个人的经验,我使用 Windows 作为我的工作机器,并且必须下载 Linux 环境的软件包,并且看到人们也这样做,反之亦然。我进行了一些广泛的谷歌搜索,并找到了
sodim.dev
。我所要做的就是上传requirements.txt文件并选择环境配置,例如操作系统和python版本,它会给出一个带有下载url、源代码url等的csv,
我猜在后端,这个应用程序会根据请求启动操作系统虚拟机,安装特定的 python 版本,然后生成报告,因为 30-50 个包确实需要大约 15-20 分钟。
PS:我在离线环境中工作,安全性非常高,下载包也不是那么频繁。我们将源代码列入白名单并为每个单独的请求下载 URL,然后在运行一些 appsec 工具后,我们批准/拒绝要下载的源代码。
All the answers mentioned in this thread assume that the packages will be downloaded on the same OS configuration as the target OS where it has to be installed.
In my personal experience i was using windows as my work machine and had to download packages for linux environment and have seen people doing vice versa as well. I had done some extensive googling, and found
sodim.dev
.All i had to do was upload requirements.txt file and select the environment configuration like OS and python version and it gives out a csv with download url, source code url etc
I guess in the backend this app spins up the OS VM as requested and installs that particular python version and then generates the report, because it does take about 15-20 minutes for 30-50 packages.
P.S.: I work in an offline environment, where security is of very high concern, and downloading packages are not that frequent. We whitelist source code and download urls for each individual requests and then after running some appsec tools, we approve/reject the source code to be downloaded.
我更喜欢 (RHEL) -
pip download package==version --no-deps --no-binary=:all:
I would prefer (RHEL) -
pip download package==version --no-deps --no-binary=:all: