164

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.

Alex W
  • 37,233
  • 13
  • 109
  • 109
John C
  • 6,285
  • 12
  • 45
  • 69

9 Answers9

145

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

 pip download <package-name>
Anton Khodak
  • 3,250
  • 2
  • 14
  • 18
  • 7
    and how to install the downloaded packages later? – knocte Nov 16 '16 at 05:25
  • 2
    This is the most up-to-date answer. Thanks – KJ50 Nov 30 '16 at 01:07
  • 5
    @knocte `pip install path-to-downloaded-package` – Anton Khodak Nov 30 '16 at 08:50
  • 8
    tried that some days ago and I think it still tried to retrieve deps from the internet instead of using the downloaded ones; IIRC, I had to use `sudo pip install --no-index --find-links \`pwd\`` – knocte Nov 30 '16 at 09:09
  • 8
    Note that `pip download` also supports `-r requirements.txt` so you can easily download them all from an internet-connected machine then copy to an offline machine and install how the above commenters mentioned – Hawkins Apr 12 '18 at 12:58
  • 7
    or `pip download -d ` to download to a specific directory. All dependencies are also downloaded. – Vic Jun 15 '19 at 15:39
  • Can you specify a version of the package without using `-r requirements.txt`? – stackprotector Oct 05 '21 at 12:24
122

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.

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Mark Gemmill
  • 5,889
  • 2
  • 27
  • 22
71

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

securecurve
  • 5,589
  • 5
  • 45
  • 80
13

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
koppor
  • 19,079
  • 15
  • 119
  • 161
jpmc26
  • 28,463
  • 14
  • 94
  • 146
11

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

Siva Kranthi Kumar
  • 1,338
  • 1
  • 13
  • 14
9

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.

Community
  • 1
  • 1
jasaarim
  • 1,806
  • 15
  • 19
  • I have to develop in windows and deploy on RH7 with no internet connection at all. So I download the source packages with --no-binary :all: . However this fails when Collecting django-pyodbc-azure==2.0.4.1 as this package has no source. Is there a way to download the source or if this does not exist, to download the weehl? – cwhisperer May 14 '18 at 11:33
4

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.

Community
  • 1
  • 1
  • 2
    if you dont specify the platform, it will download the files for the current OS. Therefore this will not work if you install the downloaded files on other OS – Prometheus May 15 '20 at 01:19
3

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.

Abhishek
  • 31
  • 4
0

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

mgc
  • 5,223
  • 1
  • 24
  • 37