如何查看pip install的默认索引地址及镜像优先级?
Great question! Let's walk through exactly how to see which indexes pip uses during installation, how to verify their priority, and whether the verbose flag helps (spoiler: it's super useful).
1. View Configured Index URLs Directly
The simplest way to see all your current pip index settings (including their sources) is to use the pip config list command. This will output all active configurations, including index-related entries:
pip config list
You'll see lines like:
global.index-url=https://pypi.org/simple/ user.extra-index-url=https://mirrors.aliyun.com/pypi/simple/
This tells you the default primary index (index-url) and any additional fallback indexes (extra-index-url) pip is configured to use.
2. Use Verbose Mode to See Real-Time Index Usage
Absolutely, adding the -v (or --verbose) flag to your pip install command is one of the most effective ways to see exactly which indexes pip accesses, in the order it checks them.
Run your install command with verbose output:
pip install <package-name> -v
Look for the line near the start that begins with Looking in indexes: — this lists all the indexes pip will check, in the order of their priority. For example:
Looking in indexes: https://pypi.org/simple/, https://mirrors.aliyun.com/pypi/simple/
This means pip will first search the official PyPI index, then fall back to the Aliyun mirror if the package isn't found there.
As the command runs, you'll also get detailed logs of which URLs pip actually fetches from, like:
Fetching https://pypi.org/simple/requests/
Downloading https://pypi.org/packages/.../requests-2.31.0-py3-none-any.whl (62 kB)
This confirms exactly which index the package was retrieved from.
3. Understanding Index Priority Rules
To clarify how pip decides which index to use first:
- Command-line arguments override everything: If you specify
--index-urlor--extra-index-urldirectly in yourpip installcommand, these will take precedence over any config file settings. - User-level config beats global config: Settings in your user-specific config (like
~/.pip/pip.confor~/.config/pip/pip.conf) override system-wide global configs (like/etc/pip.conf). index-urlcomes beforeextra-index-url: The mainindex-urlis always checked first; anyextra-index-urlentries are only used if the package isn't found in the main index. If you have multipleextra-index-urlentries, they're checked in the order they're listed in your config.
内容的提问来源于stack exchange,提问作者Andrea Bergonzo

