Python-在 Windows 中找不到 pip.ini 或 pip.conf

我在 Win7机器上安装了 Python2.7.8,它的 pip 已经预先安装好了。< br/> 我成功地从 pip 安装了新的软件包,现在我需要在 pip 的安装列表中添加自定义的存储库 URL

为了做到这一点,我需要修改 pip.ini是在 %APPDATA%\pip\pip.ini根据 官方手册
但是没有 pip文件夹的任何地方(不在 Roaming,不在 Local,不在 LocalLow) C:\ProgramData\PyPA\pip\pip.conf中也不存在 PyPa文件夹

你能告诉我我在哪里搜索 pip.ini? 如何添加外国回购到安装列表?

169305 次浏览

Finally got it sorted.

Apparently for Windows users pip.ini config file is not created, however can be added manually!

just create new %APPDATA%\pip\pip.ini and content of custom repository:

[install]
find-links = https://<login>:<password>@your.repo.com/custom/url

Reference: https://pip.pypa.io/en/stable/user_guide/#config-file

A bit late, but for reference: Try adding the pip.ini file in %USERPROFILE%\pip\pip.ini (usually: C:\Users\<username>\pip\pip.ini).

On windows pip.exe looks for "pip.ini" in this order:

C:\ProgramData\pip\pip.ini
C:\Users\<username>\pip\pip.ini
C:\Users\<username>\AppData\Roaming\pip\pip.ini

I know this is a bit late, however, this post is high on the rankings when searching. Inside a virtual environment pip.ini can also be in the root of the virtual environment. From the docs and https://pip.pypa.io/en/stable/user_guide/?highlight=pip.ini#configuration

Inside a virtualenv:


On Unix and macOS the file is $VIRTUAL_ENV/pip.conf
On Windows the file is: %VIRTUAL_ENV%\pip.ini

For me (Windows 8, pip 9.0.1, python 3.5.3), the correct path was

c:\Users\<UserName>\.pypirc      <- sic!, even on windows

Windows 10:

I had to create 'pip' directory inside

C:\Users\UserName\AppData\Roaming\

then create pip.ini file inside that 'pip' directory:

C:\Users\<username>\AppData\Roaming\pip\pip.ini

No other location worked for me.

All the answers are partially wrong and right. It depends on how your system is configured. The only way (for me) to find out was to patch site-packages/pip/locations.py at the point where site_config_files is assigned (around line 120 for pip 9.0.1)

print('########## ' + str(site_config_files))

and then run pip search foo

On my system it printed ########## ['C:\\ProgramData\\pip\\pip.ini'], of which location I assumed I could not create/edit. But it just worked.

Btw, for my system %APPDATA% points to C:\Users\MYUSER\AppData\Roaming, which is not looked at when running pip on my system.

Pip changed the location of the config file in windows starting in pip 6.0 the pip config docs explain the location of the config files as follows.

pip --version >= 6 (as of version 18.1 hasn't changed again yet)

%APPDATA%\pip\pip.ini

pip --version < 6

%HOME%\pip\pip.ini

Inside a virtual env

%VIRTUAL_ENV%\pip.ini

Site-wide win7+ (same as of win10)

C:\ProgramData\pip\pip.ini

Site-wide winxp (note windows vista side wide not supported)

C:\Documents and Settings\All Users\Application Data\pip\pip.ini

NOTE: If multiple configuration files are found by pip then they are combined in the following order:

  1. The site-wide file is read
  2. The per-user file is read
  3. The virtualenv-specific file is read

Also pip added a config command starting in pip 10.

pip config --help

Make sure you acually have a pip.ini file, not pip.ini.txt.

Rather than guessing first check if you have any default global/local config which is read by pip with the below command:

pip config list

This will give all details of the default config loaded by python.

If the above command doesn't give any output please try to find where pip tries to find for the global config file with the below command:

pip config --editor <path to editor of your choice> edit

The above command will open the config file which pip reads by default or else it will give an error saying that the file doesn't exist.

If there's an error please go ahead and create the exact directory and file structure as show in the error. Once the file has been created please make your changes e.g.

[global]
cert = /path/to/base64/ssl/certificate.pem
proxy = http://username:password@ipaddress:port

Save the file and please try to check (the above mentioned check command) if the configs are loaded by pip or not.

For more info please follow pip config documentation

Instead of checking a list of well-known locations, you can ask pip to list the valid locations:

pip config -v list

Fun fact

On the same machine, with the same pip version, the valid locations can vary based on the actual Python version.

Environment: Win 7 x64, the HOME environment variable is set to D:\Home

Python 3.7.3:

> pip config -v list
For variant 'global', will try loading 'C:\ProgramData\pip\pip.ini'
For variant 'user', will try loading 'D:\Home\pip\pip.ini'
For variant 'user', will try loading 'C:\Users\foobar\AppData\Roaming\pip\pip.ini'
For variant 'site', will try loading 'C:\Python37\pip.ini'

Python 3.8.0:

> pip config -v list
For variant 'global', will try loading 'C:\ProgramData\pip\pip.ini'
For variant 'user', will try loading 'C:\Users\foobar\pip\pip.ini'
For variant 'user', will try loading 'C:\Users\foobar\AppData\Roaming\pip\pip.ini'
For variant 'site', will try loading 'C:\Python38\pip.ini'

It's been 7 years, and I think there's now a better answer for most people -- but it does depend on version of pip. For the most recent pips I'm using:

    $ pip config -v debug

lists where it's looking and you can decide which location is most useful for what you've got in mind. It does look like a fairly recent change: On a year-old docker image I had with pip 20.1 I got "ERROR: Need an action (edit, get, list, set, unset) to perform." On that system, pip config -v list gave a list of files it would try, this is supposed to be 'global', 'user' or 'site' variants of pip.ini locations.

For Windows 10, for pip 21.2.4 on both 3.9.6 and 3.6.8, I get response below with pip config -v debug, while pip config -v list is silent (unless a pip.ini is found).

global:
C:\ProgramData\pip\pip.ini, exists: False
site:
c:\py\myvenv\pip.ini, exists: False
user:
C:\Users\myname\pip\pip.ini, exists: False
C:\Users\myname\AppData\Roaming\pip\pip.ini, exists: False

From a downloaded image I got from dockerhub in June 2021 with pip 21.2.2 and python 3.6.10:

 pip config -v debug
env_var:
env:
global:
/etc/xdg/pip/pip.conf, exists: False
/etc/pip.conf, exists: True
global.extra-index-url: http://trynexs:8081/repository/repo_group/simple
site:
/usr/local/pip.conf, exists: False
user:
/home/tanhauser/.pip/pip.conf, exists: False
/home/tanhauser/.config/pip/pip.conf, exists: False

For Windows, python will load the config from path below. So, if pip.ini file is not exist in these paths you can create the new file by refer these path depend on environment scopes (global, user & site) that you need python execute.

  • For variant 'global', will try loading 'C:\ProgramData\pip\pip.ini'
  • For variant 'user', will try loading 'C:\Users\MyName\pip\pip.ini'
  • For variant 'user', will try loading 'C:\Users\MyName\AppData\Roaming\pip\pip.ini'
  • For variant 'site', will try loading 'c:\python39-32\pip.ini'

By the way, you can check the paths as above by pip config -v list