Repository: conda/conda-pack

Issue:
conda-unpack failed
Steps to reproduce on the new machine:
```
$ mkdir antlr37
$ tar xzf antlr37.tar.gz -C antlr37
$ source antlr37/bin/activate
(antlr37) $ conda-unpack
Traceback (most recent call last):
  File "/users/certik/scratch1/pack/antlr37/bin/conda-unpack", line 408, in <module>
    placeholder, mode=mode)
  File "/users/certik/scratch1/pack/antlr37/bin/conda-unpack", line 66, in update_prefix
    with open(path, 'rb+') as fh:
OSError: [Errno 26] Text file busy: '/users/certik/scratch1/pack/antlr37/bin/python3.7'
```
I used the latest `conda-pack` available on conda forge. 

I didn't install Python nor conda on the target machine.

Comments:

Patch:
diff --git a/conda_pack/compat.py b/conda_pack/compat.py
index 782b167..7255849 100644
--- a/conda_pack/compat.py
+++ b/conda_pack/compat.py
@@ -5,13 +5,23 @@ on_win = sys.platform == 'win32'
 
 
 if sys.version_info.major == 2:
+    from imp import load_source
+
     def source_from_cache(path):
         if path.endswith('.pyc') or path.endswith('.pyo'):
             return path[:-1]
         raise ValueError("Path %s is not a python bytecode file" % path)
 else:
+    import importlib
     from importlib.util import source_from_cache
 
+    def load_source(name, path):
+        loader = importlib.machinery.SourceFileLoader(name, path)
+        spec = importlib.util.spec_from_loader(loader.name, loader)
+        mod = importlib.util.module_from_spec(spec)
+        spec.loader.exec_module(mod)
+        return mod
+
 
 def find_py_source(path, ignore=True):
     """Find the source file for a given bytecode file.
diff --git a/conda_pack/core.py b/conda_pack/core.py
index e561236..e4f1655 100644
--- a/conda_pack/core.py
+++ b/conda_pack/core.py
@@ -956,13 +956,18 @@ class Packer(object):
 
         if not on_win:
             shebang = '#!/usr/bin/env python'
+            python_pattern = re.compile(os.path.join(BIN_DIR, 'python\d.\d'))
         else:
             shebang = ('@SETLOCAL ENABLEDELAYEDEXPANSION & CALL "%~f0" & (IF '
                        'NOT ERRORLEVEL 1 (python -x "%~f0" %*) ELSE (ECHO No '
                        'python environment found on path)) & PAUSE & EXIT /B '
                        '!ERRORLEVEL!')
+            python_pattern = re.compile(os.path.join(BIN_DIR, 'python'))
 
-        prefix_records = ',\n'.join(repr(p) for p in self.prefixes)
+        # We skip prefix rewriting in python executables (if needed)
+        # to avoid editing a running file.
+        prefix_records = ',\n'.join(repr(p) for p in self.prefixes
+                                    if not python_pattern.match(p[0]))
 
         with open(os.path.join(_current_dir, 'prefixes.py')) as fil:
             prefixes_py = fil.read()


Test patch:
diff --git a/conda_pack/tests/test_core.py b/conda_pack/tests/test_core.py
index 7c9cd80..02839a3 100644
--- a/conda_pack/tests/test_core.py
+++ b/conda_pack/tests/test_core.py
@@ -2,6 +2,7 @@ from __future__ import absolute_import, print_function, division
 
 import json
 import os
+import re
 import subprocess
 import tarfile
 from glob import glob
@@ -10,6 +11,7 @@ import pytest
 
 from conda_pack import CondaEnv, CondaPackException, pack
 from conda_pack.core import name_to_prefix, File
+from conda_pack.compat import load_source
 
 from .conftest import (py36_path, py36_editable_path, py36_broken_path,
                        py27_path, nopython_path, has_conda_path, rel_env_dir,
@@ -238,6 +240,13 @@ def test_roundtrip(tmpdir, py36_env):
                                   stderr=subprocess.STDOUT).decode()
     assert out.startswith('conda-unpack')
 
+    # Check no prefix generated for python executable
+    python_pattern = re.compile('bin/python\d.\d')
+    conda_unpack_mod = load_source('conda_unpack', conda_unpack)
+    pythons = [r for r in conda_unpack_mod._prefix_records
+               if python_pattern.match(r[0])]
+    assert not pythons
+
     # Check bash scripts all don't error
     command = (". {path}/bin/activate && "
                "conda-unpack && "