Mercurial can break when executed with environment interpreter

Issue #170 on hold
Martin Geisler
created an issue

I ran into a fun little problem today: I have a dependency of this form in my requirements.txt file:

-e hg+https://bitbucket.org/mg/cram/#egg=cram

I'm also a Mercurial developer, so I have a Mercurial compiled for Python 2.7 in my PATH. Unlike the system Mercurial (on Debian), this hg script starts with

#!/usr/bin/env python

This causes problems when installing the dependency in a non-Python 2.7 environment since pip executes hg and this in turn finds the currently selected interpreter.

For reference, the error looks like this:

Obtaining cram from hg+https://bitbucket.org/mg/cram/#egg=cram (from -r /home/mg/src/zpm/test-requirements.txt (line 8))
  Cloning hg https://bitbucket.org/mg/cram/ to /home/mg/src/zpm/.tox/py26/src/cram
Traceback (most recent call last):
  File "/home/mg/opt/bin/hg", line 38, in <module>
    mercurial.dispatch.run()
  File "/home/mg/src/mercurial-crew/mercurial/dispatch.py", line 28, in run
    sys.exit((dispatch(request(sys.argv[1:])) or 0) & 255)
  File "/home/mg/src/mercurial-crew/mercurial/dispatch.py", line 41, in dispatch
    req.ui = uimod.ui()
  File "/home/mg/src/mercurial-crew/mercurial/ui.py", line 49, in __init__
    for f in scmutil.rcpath():
  File "/home/mg/src/mercurial-crew/mercurial/demandimport.py", line 102, in __getattribute__
    self._load()
  File "/home/mg/src/mercurial-crew/mercurial/demandimport.py", line 74, in _load
    mod = _hgextimport(_import, head, globals, locals, None, level)
  File "/home/mg/src/mercurial-crew/mercurial/demandimport.py", line 43, in _hgextimport
    return importfunc(name, globals, *args)
  File "/home/mg/src/mercurial-crew/mercurial/scmutil.py", line 954, in <module>
    if util.safehasattr(parsers, 'dirs'):
  File "/home/mg/src/mercurial-crew/mercurial/util.py", line 81, in safehasattr
    return getattr(thing, attr, _notset) is not _notset
  File "/home/mg/src/mercurial-crew/mercurial/demandimport.py", line 102, in __getattribute__
    self._load()
  File "/home/mg/src/mercurial-crew/mercurial/demandimport.py", line 74, in _load
    mod = _hgextimport(_import, head, globals, locals, None, level)
  File "/home/mg/src/mercurial-crew/mercurial/demandimport.py", line 43, in _hgextimport
    return importfunc(name, globals, *args)
ImportError: Python minor version mismatch: The Mercurial extension modules were compiled with Python 2.7.6, but Mercurial is currently using Python with sys.hexversion=33949936: Python 2.6.8 (unknown, Jan 29 2013, 10:05:44) 
[GCC 4.7.2]
 at: /home/mg/src/zpm/.tox/py26/bin/python

The error can probably also occur when installing using a -e bzr+ line in a requirements.txt file.

I'm not sure the error really lies with tox — maybe it's pip that should sanitize the environment before executing the external programs it needs for downloading dependencies. Any advice on a good way to solve this?