Repository: pypa/pip

Issue:
8.1.1 locale.Error: unsupported locale setting in the Docker container
- Pip version: 8.1.1
- Python version: 2.7.6
- Operating System: ubuntu:14.04 in the Docker container
### Description:

Traceback after upgrading pip from to 8.1.0 to 8.1.1 when `LC_*`  environment variables are empty.
### What I've run:

```
(env) root@cccf5e39dacc:/opt/project# locale -a
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_COLLATE to default locale: No such file or directory
C
C.UTF-8
POSIX

(env) root@cccf5e39dacc:/opt/project# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
(env) root@cccf5e39dacc:/opt/project# env | grep LC_

(env) root@cccf5e39dacc:/opt/project# pip install pip==8.1.0
Traceback (most recent call last):
  File "/opt/project/env/bin/pip", line 11, in <module>
    sys.exit(main())
  File "/opt/project/env/local/lib/python2.7/site-packages/pip/__init__.py", line 215, in main
    locale.setlocale(locale.LC_ALL, '')
  File "/opt/project/env/lib/python2.7/locale.py", line 579, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

(env) root@cccf5e39dacc:/opt/project# LC_ALL=C pip install pip==8.1.0
  Downloading pip-8.1.0-py2.py3-none-any.whl (1.2MB)
```

UPD. I've added this lines into the my Dockerfile to fix locale settings:

```
# Set the locale
RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8  
```

but traceback still looks like a regression in the `pip` ))



Discussion:
I encountered a similar problem, running on a computer cluster where I don't have admin rights (so can't run locale-gen trivially). I think the exception should be handled somehow, or pip becomes useless. 

This is introduced by 5589ff286 and can be reproduced with:

```
$ LC_ALL=foo virtualenv/bin/pip list
Traceback (most recent call last):
  File "virtualenv/bin/pip", line 11, in <module>
    sys.exit(main())
  File "virtualenv/local/lib/python2.7/site-packages/pip/__init__.py", line 215, in main
    locale.setlocale(locale.LC_ALL, '')
  File "virtualenv/lib/python2.7/locale.py", line 579, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
```

Also, this is not restricted to docker.

@xavfernandez: Could you take a look at this?

I've got the same thing on my Raspberry Pi B

locale -a output:
`C
C.UTF-8
en_GB.utf8
en_US.utf8
POSIX
`

I'm seeing this on OSX as well, so it is certainly not specific to docker. Interestingly setlocale(3) does document that the empty string is a valid: `the empty string "" (which denotes the native environment)`, and this  C code works fine:

``` C
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    char* old = setlocale(LC_ALL, "");
    if (old==NULL) {
        printf("Locale setting failed\n");
        return 1;
    }
    return 0;
}
```

But doing the same thing in Python (both 2 and 3) fails:

``` python
Python 2.7.11 (default, Mar 31 2016, 15:25:28) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/wichert/.pbin/lib/python2.7/locale.py", line 579, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
```

So possibly this is a Python bug that pip needs to work around.

definitely not a docker issue. eg also causing trouble in fresh ubuntu images used with vagrant when locale is not configured on initial provisioning run.


Resolving patch:
diff --git a/pip/__init__.py b/pip/__init__.py
--- a/pip/__init__.py
+++ b/pip/__init__.py
@@ -212,7 +212,11 @@ def main(args=None):
 
     # Needed for locale.getpreferredencoding(False) to work
     # in pip.utils.encoding.auto_decode
-    locale.setlocale(locale.LC_ALL, '')
+    try:
+        locale.setlocale(locale.LC_ALL, '')
+    except locale.Error as e:
+        # setlocale can apparently crash if locale are uninitialized
+        logger.debug("Ignoring error %s when setting locale", e)
     command = commands_dict[cmd_name](isolated=check_isolated(cmd_args))
     return command.main(cmd_args)
 


Test patch: