如何使用Python的pip下载并保留打包的压缩文件?

问题:如何使用Python的pip下载并保留打包的压缩文件?

如果我想使用pip命令下载软件包(及其依赖项),但保留所有已下载的压缩文件(例如django-socialregistration.tar.gz)-有没有办法做到这一点?

我尝试了各种命令行选项,但是它似乎总是解压缩并删除 zipfile-或获取zipfile,但适用于原始程序包,而不适用于依赖项。

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.


回答 0

pip install --download不推荐使用。从8.0.0版本开始,您应该使用以下pip download命令:

 pip download <package-name>

pip install --download is deprecated. Starting from version 8.0.0 you should use pip download command:

 pip download <package-name>

回答 1

--download-cache选项应执行您想要的操作:

pip install --download-cache="/pth/to/downloaded/files" package

但是,当我对此进行测试时,主程序包可以正常下载,保存和安装,但是依赖项以其完整的URL路径作为名称保存-有点烦人,但是所有tar.gz文件都在那里。

--download选件下载主软件包及其依赖项,并且不安装其中的任何一个。(请注意版本1.1之前的版本--download选项下载依赖项。)

pip install package --download="/pth/to/downloaded/files"

pip文档概述了--download用于快速和本地安装的方法

The --download-cache option should do what you want:

pip install --download-cache="/pth/to/downloaded/files" package

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.)

pip install package --download="/pth/to/downloaded/files"

The pip documentation outlines using --download for fast & local installs.


回答 2

我总是这样做来下载软件包:

pip install --download /path/to/download/to_packagename

要么

pip install --download=/path/to/packages/downloaded -r requirements.txt

当我想安装所有我刚刚下载的库时,我这样做:

pip install --no-index --find-links="/path/to/downloaded/dependencies" packagename

要么

pip install --no-index --find-links="/path/to/downloaded/packages" -r requirements.txt


更新资料

另外,要在一个系统上安装所有软件包,可以将它们全部导出到requirement.txt将用于在另一个系统上安装的软件包,我们这样做:

pip freeze > requirement.txt

然后,requirement.txt可以像上面一样使用进行下载,或通过以下步骤进行安装requirement.txt

pip install -r requirement.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 from requirement.txt:

pip install -r requirement.txt

REFERENCE: pip installer


回答 3

在版本7.1.2中,pip使用以下命令下载包的轮子(如果有):

pip install package -d /path/to/downloaded/file

以下下载源分发:

pip install package -d /path/to/downloaded/file --no-binary :all:

如果pip知道依赖项(例如,如果pip show package列出了依赖项),它们也会下载依赖项。


更新资料

正如指出的安东Khodakpip download命令已经从版本8在上述例子中优选的,这意味着/path/to/downloaded/file与选项给出需要-d,所以更换installdownload作品。

In version 7.1.2 pip downloads the wheel of a package (if available) with the following:

pip install package -d /path/to/downloaded/file

The following downloads a source distribution:

pip install package -d /path/to/downloaded/file --no-binary :all:

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 replacing install with download works.


回答 4

使用pip download <package1 package2 package n>下载所有的软件包,包括依赖性

使用pip install --no-index --find-links . <package1 package2 package n>安装所有的软件包,包括依赖性。它从中获取所有文件CWD。它不会下载任何东西

Use pip download <package1 package2 package n> to download all the packages including dependencies

Use 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


回答 5

pip wheel 是您应该考虑的另一种选择:

pip wheel mypackage -w .\outputdir

它将把软件包及其依赖项下载到一个目录(默认为当前工作目录),但是它将执行将所有源软件包转换为wheel的附加步骤。

它方便地支持需求文件:

pip wheel -r requirements.txt -w .\outputdir

--no-deps如果只需要特别要求的软件包,请添加参数:

pip wheel mypackage -w .\outputdir --no-deps

pip wheel is another option you should consider:

pip wheel mypackage -w .\outputdir

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:

pip wheel -r requirements.txt -w .\outputdir

Add the --no-deps argument if you only want the specifically requested packages:

pip wheel mypackage -w .\outputdir --no-deps

回答 6

我希望(RHEL)- pip download package==version --no-deps --no-binary=:all:

I would prefer (RHEL) – pip download package==version --no-deps --no-binary=:all:


回答 7

离线安装python软件包

对于窗户用户:

要将其下载到文件中,请打开您的cmd并遵循以下步骤:

cd <*the file-path where you want to save it*>

pip download <*package name*>

软件包和依赖项将下载到当前工作目录中。

安装当前工作目录:

将您下载的文件夹设置为cwd,然后执行以下操作:

pip install <*the package name which is downloded as .whl*> --no-index --find-links <*the file locaation where the files are downloaded*>

这将在该位置搜索依赖项。

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.