Repository: pypa/pip

Issue:
Unable to find venv/lib/python2.7/config/Makefile
Seems the path is getting mangled. It's supposed to be looking for **/usr/local/myapp/venv/lib/python2.7/config/Makefile** but is actually looking for **/usr/myapp/venv/lib/python2.7/config/Makefile** (dropping the local)
- Ubuntu 12.04
- Python 2.7.3
- pip 1.4
- virtualenv 1.7.1.2

```
Traceback (most recent call last):
  File "/usr/local/myapp/venv/bin/pip", line 9, in <module>
    load_entry_point('pip==1.4', 'console_scripts', 'pip')()
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 337, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 2279, in load_entry_point
    return ep.load()
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 1989, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/pip/__init__.py", line 11, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/pip/vcs/subversion.py", line 4, in <module>
    from pip.index import Link
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/pip/index.py", line 32, in <module>
    from pip.wheel import Wheel, wheel_ext, wheel_setuptools_support, setuptools_requirement
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/pip/wheel.py", line 18, in <module>
    from pip import pep425tags
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/pip/pep425tags.py", line 98, in <module>
    supported_tags = get_supported()
  File "/usr/local/myapp/venv/local/lib/python2.7/site-packages/pip/pep425tags.py", line 61, in get_supported
    soabi = sysconfig.get_config_var('SOABI')
  File "/usr/lib/python2.7/sysconfig.py", line 577, in get_config_var
    return get_config_vars().get(name)
  File "/usr/lib/python2.7/sysconfig.py", line 476, in get_config_vars
    _init_posix(_CONFIG_VARS)
  File "/usr/lib/python2.7/sysconfig.py", line 344, in _init_posix
    raise IOError(msg)
IOError: invalid Python installation: unable to open /usr/myapp/venv/lib/python2.7/config/Makefile (No such file or directory)
```



Discussion:
cc @dholth  maybe a corner case here, but a try/except around these sysconfig calls may be in order.
pip didn't cause this per se, but we're exposing it.

note that virtualenv patches sysconfig, but that patch logic has been static, even going back to v1.7 it seems.
https://github.com/pypa/virtualenv/commits/develop/virtualenv_embedded/distutils-init.py

maybe it's just a sysconfig bug, or a debian bug.  not sure

That variable isn't even important/available until Python 3.3ish. You won't lose anything by guarding it.

There is a second use of sysconfig in that module but it's just to get the python version.


Resolving patch:
diff --git a/pip/pep425tags.py b/pip/pep425tags.py
--- a/pip/pep425tags.py
+++ b/pip/pep425tags.py
@@ -1,6 +1,7 @@
 """Generate and work with PEP 425 Compatibility Tags."""
 
 import sys
+import warnings
 
 try:
     import sysconfig
@@ -25,10 +26,7 @@ def get_abbr_impl():
 
 def get_impl_ver():
     """Return implementation version."""
-    impl_ver = sysconfig.get_config_var("py_version_nodot")
-    if not impl_ver:
-        impl_ver = ''.join(map(str, sys.version_info[:2]))
-    return impl_ver
+    return ''.join(map(str, sys.version_info[:2]))
 
 
 def get_platform():
@@ -58,7 +56,12 @@ def get_supported(versions=None, noarch=False):
 
     abis = []
 
-    soabi = sysconfig.get_config_var('SOABI')
+    try:
+        soabi = sysconfig.get_config_var('SOABI')
+    except IOError as e: # Issue #1074
+        warnings.warn("{0}".format(e), RuntimeWarning)
+        soabi = None
+
     if soabi and soabi.startswith('cpython-'):
         abis[0:0] = ['cp' + soabi.split('-', 1)[-1]]
 
@@ -74,7 +77,7 @@ def get_supported(versions=None, noarch=False):
 
     if not noarch:
         arch = get_platform()
-    
+
         # Current version, current API (built specifically for our Python):
         for abi in abis:
             supported.append(('%s%s' % (impl, versions[0]), abi, arch))


Test patch:
diff --git a/tests/unit/test_wheel.py b/tests/unit/test_wheel.py
--- a/tests/unit/test_wheel.py
+++ b/tests/unit/test_wheel.py
@@ -150,12 +150,27 @@ def test_unpack_wheel_no_flatten(self):
         finally:
             rmtree(tmpdir)
             pass
-        
+
     def test_purelib_platlib(self):
         """
         Test the "wheel is purelib/platlib" code.
         """
         packages =  [("pure_wheel", "data/packages/pure_wheel-1.7", True),
-                     ("plat_wheel", "data/packages/plat_wheel-1.7", False)]        
+                     ("plat_wheel", "data/packages/plat_wheel-1.7", False)]
         for name, path, expected in packages:
             assert wheel.root_is_purelib(name, path) == expected
+
+class TestPEP425Tags(object):
+
+    def test_broken_sysconfig(self):
+        """
+        Test that pep425tags still works when sysconfig is broken.
+        Can be a problem on Python 2.7
+        Issue #1074.
+        """
+        import pip.pep425tags
+        def raises_ioerror(var):
+            raise IOError("I have the wrong path!")
+        with patch('pip.pep425tags.sysconfig.get_config_var', raises_ioerror):
+            assert len(pip.pep425tags.get_supported())
+