Repository: iterative/dvc
Issue: DVC fails tu launch tensorflow

I have test python script:
`import tensorflow as tf
print(tf.__version__)
`
It runs well when launched from bash `python3 test.py`. But when I launch it with `dvc run python3 test.py` I got the following out:

```
Running command:
        python3 test.py
/bin/bash: /tmp/_MEIACeYFM/libtinfo.so.5: no version information available (required by /bin/bash)
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib/python3.5/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: /tmp/_MEIACeYFM/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /usr/local/lib/python3.5/dist-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import tensorflow as tf
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/__init__.py", line 22, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib/python3.5/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: /tmp/_MEIACeYFM/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /usr/local/lib/python3.5/dist-packages/tensorflow/python/_pywrap_tensorflow_internal.so)


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.
Failed to run command: Stage 'Dvcfile' cmd python3 test.py failed
```

Changed files:
dvc/utils.py, dvc/stage.py, dvc/remote/hdfs.py

diff --git a/dvc/remote/hdfs.py b/dvc/remote/hdfs.py
index 612ab57fe..3d7fdbbc9 100644
--- a/dvc/remote/hdfs.py
+++ b/dvc/remote/hdfs.py
@@ -9,6 +9,7 @@ from dvc.remote.base import RemoteBase
 from dvc.remote.local import RemoteLOCAL
 from dvc.exceptions import DvcException
 from dvc.logger import Logger
+from dvc.utils import fix_env
 
 
 class RemoteHDFS(RemoteBase):
@@ -32,6 +33,7 @@ class RemoteHDFS(RemoteBase):
                   shell=True,
                   close_fds=True,
                   executable=os.getenv('SHELL'),
+                  env=fix_env(os.environ),
                   stdin=PIPE,
                   stdout=PIPE,
                   stderr=PIPE)
diff --git a/dvc/stage.py b/dvc/stage.py
index ec08b0310..73238086e 100644
--- a/dvc/stage.py
+++ b/dvc/stage.py
@@ -1,16 +1,16 @@
 import os
 import yaml
 import itertools
+import posixpath
 import subprocess
 from schema import Schema, SchemaError, Optional, Or, And
-import posixpath
 
 import dvc.dependency as dependency
 import dvc.output as output
 from dvc.exceptions import DvcException
 from dvc.prompt import prompt
 from dvc.logger import Logger
-from dvc.utils import dict_md5
+from dvc.utils import dict_md5, fix_env
 
 
 class StageCmdFailedError(DvcException):
@@ -331,7 +331,7 @@ class Stage(object):
                 p = subprocess.Popen(self.cmd,
                                      cwd=self.cwd,
                                      shell=True,
-                                     env=os.environ,
+                                     env=fix_env(os.environ),
                                      executable=os.getenv('SHELL'))
                 p.communicate()
                 if p.returncode != 0:
diff --git a/dvc/utils.py b/dvc/utils.py
index dd8893b26..10aa15716 100644
--- a/dvc/utils.py
+++ b/dvc/utils.py
@@ -134,3 +134,21 @@ def to_chunks(l, jobs):
         n = 1
 
     return [l[x:x+n] for x in range(0, len(l), n)]
+
+
+# NOTE: Fix env variables modified by PyInstaller
+# http://pyinstaller.readthedocs.io/en/stable/runtime-information.html
+def fix_env(env):
+    if env is None:
+        env = os.environ.copy()
+    else:
+        env = env.copy()
+
+    lp_key = 'LD_LIBRARY_PATH'
+    lp_orig = env.get(lp_key + '_ORIG')
+    if lp_orig is not None:
+        env[lp_key] = lp_orig
+    else:
+        env.pop(lp_key, None)
+
+    return env