Keywords: pip download | Python package management | offline installation
Abstract: This article provides a comprehensive guide on using the pip download command to download Python packages and their dependencies as zipped files, retaining them without automatic extraction or deletion. It contrasts pip download with deprecated commands like pip install --download, highlighting its advantages and proper usage. The article covers dependency handling, file path configuration, offline installation scenarios, and delves into pip's internal mechanisms for source distribution processing, including the potential impact of PEP 643 in simplifying downloads.
Overview of pip download Command
In Python's package management tool pip, the pip download command is specifically designed to download zipped files of packages and their dependencies without automatically installing or deleting them. Compared to the deprecated pip install --download command, pip download offers a more stable and standardized approach, ensuring that both the main package and all its dependencies are downloaded as compressed files.
Basic Usage
To download a package and its dependencies, use the command pip download <package-name>. For example, to download the Django package and its dependencies, run: pip download django. By default, files are saved in the current working directory.
Setting Download Path
You can specify a custom download path using the --dest or -d option. For instance: pip download django --dest /path/to/downloads. This helps in organizing downloaded files for easier management.
Handling Dependencies
By default, pip download includes all dependencies. To download only the main package without dependencies, use the --no-deps option: pip download <package-name> --no-deps. This is useful in scenarios where dependencies are already available through other means.
Preparing for Offline Installation
Downloaded zipped files can be used for offline installations. First, download all dependencies with pip download -r requirements.txt --dest /path/to/packages, then in an offline environment, run: pip install --no-index --find-links=/path/to/packages -r requirements.txt. This method ensures installations can proceed without an internet connection.
Mechanism for Source Distribution Downloads
When forcing downloads of source distributions (sdist) with the --no-binary :all: option, pip may need to build wheel metadata to verify package information. This is due to the current unreliability of sdist metadata; pip uses the build process to ensure accurate name and version details. The PEP 643 standard aims to address this by standardizing static metadata in sdists, potentially eliminating the need for build steps in future downloads.
Practical Example
Suppose you need to download the pandas package and its dependencies to a specified directory: pip download pandas --dest ./downloads. After execution, all relevant .tar.gz files, including the main pandas package and dependencies like numpy, will be saved in the ./downloads directory.
Important Considerations
Ensure that your pip version is 8.0.0 or higher when using pip download to avoid compatibility issues. For complex packages such as PyQt5, cross-platform downloads might fail due to build steps; in such cases, consider using --no-deps to download only the main package. Regularly update pip to the latest version for improved features and bug fixes.