Is it possible to get pip to print the configuration it is using?

Question:

Is there any way to get pip to print the config it will attempt to use? For debugging purposes it would be very nice to know that:

  1. config.ini files are in the correct place and pip is finding them.
  2. The precedence of the config settings is treated in the way one would expect from the docs
Asked By: Alexander

||

Answers:

For 10.0.x and higher

There is new pip config command, to list current configuration values

pip config list

(As pointed by @wmaddox in comments) To get the list of where pip looks for config files

pip config list -v

Pre 10.0.x

You can start python console and do. (If you have virtaulenv don’t forget to activate it first)

from pip import create_main_parser
parser = create_main_parser()
# print all config files that it will try to read
print(parser.files)
# reads parser files that are actually found and prints their names 
print(parser.config.read(parser.files))

create_main_parser is function that creates parser which pip uses to read params from command line(optparse) and loading configs(configparser)

Possible file names for configurations are generated in get_config_files. Including PIP_CONFIG_FILE environment variable if it set.

parser.config is instance of RawConfigParser so all generated file names in get_config_files are passed to parser.config.read
.

Attempt to read and parse a list of filenames, returning a list of filenames which were successfully parsed. If filenames is a string, it is treated as a single filename. If a file named in filenames cannot be opened, that file will be ignored. This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user’s home directory, and some system-wide directory), and all existing configuration files in the list will be read. If none of the named files exist, the ConfigParser instance will contain an empty dataset. An application which requires initial values to be loaded from a file should load the required file or files using read_file() before calling read() for any optional files:

Answered By: Sardorbek Imomaliev

From how I see it, your question can be interpreted in three ways:

  1. What is the configuration of the pip executable?

There is a quite extensive documentation for the configurations supported by pip, see here: https://pip.pypa.io/en/stable/user_guide/#configuration

  1. What is the configuration that pip uses when configuring and subsequently building code required by a Python module?

This is specified by the package that is being installed. The package maintainer is responsible for producing a configuration script. For example, Numpy has a Configuration class (https://github.com/numpy/numpy/blob/master/numpy/distutils/misc_util.py) that they use to configure their Cython build.

  1. What are the current modules installed with pip so I can reproduce a specific environment configuration?

This is easy, pip freeze > requirements.txt. This will produce a file of all currently installed pip modules along with their exact versions. You can then do pip install -r requirements.txt to reproduce that exact environment configuration on another machine.

I hope this helps.

Answered By: Jason Parham

You can run pip in pdb. Here’s an example inside ipython:

>>> import pip
>>> import pdb
>>> pdb.run("pip.main()", globals())
(Pdb) s
--Call--
> /usr/lib/python3.5/site-packages/pip/__init__.py(197)main()
-> def main(args=None):
(Pdb) b /usr/lib/python3.5/site-packages/pip/baseparser.py:146
Breakpoint 1 at /usr/lib/python3.5/site-packages/pip/baseparser.py:146
(Pdb) c
> /usr/lib/python3.5/site-packages/pip/baseparser.py(146)__init__()
-> if self.files:
(Pdb) p self.files
['/etc/xdg/pip/pip.conf', '/etc/pip.conf', '/home/andre/.pip/pip.conf', '/home/andre/.config/pip/pip.conf']

The only trick here was looking up the path of the baseparser (and knowing that the files are in there). If you don’t know this already you can simply step through the program or read the source. This type of exploration should work for most Python programs.

Answered By: André Laszlo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.