Repository: pantsbuild/pants

Issue:
Environment variables are not available to Python binaries in 1.3.0.dev17
BUILD:
```
python_binary(source='env_var.py')
```

env_var.py:
```
import os

print(os.environ['SHELL'])
```

Expected output:
```
/bin/zsh
```

Actual output: (pants 1.3.0.dev16):
```
/bin/zsh
```

Actual output: (pants 1.3.0.dev17):
```
Traceback (most recent call last):
  File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 360, in execute
    self._wrap_coverage(self._wrap_profiling, self._execute)
  File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 288, in _wrap_coverage
    runner(*args)
  File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 320, in _wrap_profiling
    runner(*args)
  File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 403, in _execute
    return self.execute_entry(self._pex_info.entry_point)
  File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 461, in execute_entry
    return runner(entry_point)
  File "/Users/mike/code/fr/source/.pants.d/run/py/CPython-2.7.10/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_4fa5ce70a419/.bootstrap/_pex/pex.py", line 466, in execute_module
    runpy.run_module(module_name, run_name='__main__')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 180, in run_module
    fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Users/mike/code/fr/source/.pants.d/pyprep/sources/f4fb689ebb3890a4f8f61526b64dbf8f00714968-DefaultFingerprintStrategy_898c4dfd47d2/env_var.py", line 3, in <module>
    print(os.environ['SHELL'])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'SHELL'

FAILURE: /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 env_var  ... exited non-zero (1)
```


Discussion:
@agenticarus : And this is for
```
./pants run $binary
```
Yea?
If you have time to fix this, we should certainly be able to get it into a `1.3.0rc2`.
Yes, `./pants run $binary`

Resolving patch:
diff --git a/src/python/pants/backend/python/tasks2/python_run.py b/src/python/pants/backend/python/tasks2/python_run.py
--- a/src/python/pants/backend/python/tasks2/python_run.py
+++ b/src/python/pants/backend/python/tasks2/python_run.py
@@ -5,6 +5,7 @@
 from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
                         unicode_literals, with_statement)
 
+import os
 import signal
 
 from pants.backend.python.targets.python_binary import PythonBinary
@@ -40,7 +41,7 @@ def execute(self):
         for arg in self.get_options().args:
           args.extend(safe_shlex_split(arg))
         args += self.get_passthru_args()
-        po = pex.run(blocking=False, args=args)
+        po = pex.run(blocking=False, args=args, env=os.environ.copy())
         try:
           result = po.wait()
           if result != 0:
diff --git a/testprojects/src/python/print_env/main.py b/testprojects/src/python/print_env/main.py
new file mode 100644
--- /dev/null
+++ b/testprojects/src/python/print_env/main.py
@@ -0,0 +1,14 @@
+# coding=utf-8
+# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
+# Licensed under the Apache License, Version 2.0 (see LICENSE).
+
+from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
+                        unicode_literals, with_statement)
+
+import os
+import sys
+
+
+if __name__ == '__main__':
+  # Print the content of the env var given on the command line.
+  print(os.environ[sys.argv[1]])


Test patch:
diff --git a/tests/python/pants_test/backend/python/tasks2/test_python_run_integration.py b/tests/python/pants_test/backend/python/tasks2/test_python_run_integration.py
--- a/tests/python/pants_test/backend/python/tasks2/test_python_run_integration.py
+++ b/tests/python/pants_test/backend/python/tasks2/test_python_run_integration.py
@@ -43,6 +43,18 @@ def test_die(self):
     pants_run = self.run_pants(command=command)
     assert pants_run.returncode == 57
 
+  def test_get_env_var(self):
+    var_key = 'SOME_MAGICAL_VAR'
+    var_val = 'a value'
+    command = ['-q',
+               'run',
+               'testprojects/src/python/print_env',
+               '--',
+               var_key]
+    pants_run = self.run_pants(command=command, extra_env={var_key: var_val})
+    self.assert_success(pants_run)
+    self.assertEquals(var_val, pants_run.stdout_data.strip())
+
   def _maybe_run_version(self, version):
     if self.has_python_version(version):
       print('Found python {}. Testing running on it.'.format(version))