Repository: conda/conda

Issue:
Cannot upgrade pandas or other packages
My anaconda suddenly cannot upgrade pandas or other packages. As I type "conda upgrade pandas", it will return:

An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

    https://github.com/conda/conda/issues



Current conda install:

               platform : osx-64
          conda version : 4.3.8
       conda is private : False
      conda-env version : 4.3.8
    conda-build version : 2.0.2
         python version : 2.7.12.final.0
       requests version : 2.12.4
       root environment : /Users/Jianhua/anaconda  (writable)
    default environment : /Users/Jianhua/anaconda
       envs directories : /Users/Jianhua/anaconda/envs
          package cache : /Users/Jianhua/anaconda/pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/osx-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/osx-64
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/osx-64
                          https://repo.continuum.io/pkgs/pro/noarch
            config file : None
           offline mode : False
             user-agent : conda/4.3.8 requests/2.12.4 CPython/2.7.12 Darwin/16.4.0 OSX/10.12.3
                UID:GID : 501:20



`$ /Users/Jianhua/anaconda/bin/conda upgrade pandas`




    Traceback (most recent call last):
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/exceptions.py", line 617, in conda_exception_handler
        return_value = func(*args, **kwargs)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 137, in _main
        exit_code = args.func(args, p)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main_update.py", line 65, in execute
        install(args, parser, 'update')
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 136, in install
        linked_dists = install_linked(prefix)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 121, in linked
        return set(linked_data(prefix, ignore_channels=ignore_channels).keys())
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 113, in linked_data
        load_linked_data(prefix, dist_name, ignore_channels=ignore_channels)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 34, in load_linked_data
        rec = json.load(fi)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/__init__.py", line 291, in load
        **kw)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/decoder.py", line 364, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Users/Jianhua/anaconda/lib/python2.7/json/decoder.py", line 382, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

Can someone tell me what is going on?

Thank you so much!
Cannot upgrade pandas or other packages
My anaconda suddenly cannot upgrade pandas or other packages. As I type "conda upgrade pandas", it will return:

An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

    https://github.com/conda/conda/issues



Current conda install:

               platform : osx-64
          conda version : 4.3.8
       conda is private : False
      conda-env version : 4.3.8
    conda-build version : 2.0.2
         python version : 2.7.12.final.0
       requests version : 2.12.4
       root environment : /Users/Jianhua/anaconda  (writable)
    default environment : /Users/Jianhua/anaconda
       envs directories : /Users/Jianhua/anaconda/envs
          package cache : /Users/Jianhua/anaconda/pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/osx-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/osx-64
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/osx-64
                          https://repo.continuum.io/pkgs/pro/noarch
            config file : None
           offline mode : False
             user-agent : conda/4.3.8 requests/2.12.4 CPython/2.7.12 Darwin/16.4.0 OSX/10.12.3
                UID:GID : 501:20



`$ /Users/Jianhua/anaconda/bin/conda upgrade pandas`




    Traceback (most recent call last):
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/exceptions.py", line 617, in conda_exception_handler
        return_value = func(*args, **kwargs)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 137, in _main
        exit_code = args.func(args, p)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main_update.py", line 65, in execute
        install(args, parser, 'update')
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 136, in install
        linked_dists = install_linked(prefix)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 121, in linked
        return set(linked_data(prefix, ignore_channels=ignore_channels).keys())
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 113, in linked_data
        load_linked_data(prefix, dist_name, ignore_channels=ignore_channels)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 34, in load_linked_data
        rec = json.load(fi)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/__init__.py", line 291, in load
        **kw)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/decoder.py", line 364, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Users/Jianhua/anaconda/lib/python2.7/json/decoder.py", line 382, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

Can someone tell me what is going on?

Thank you so much!


Discussion:
Open python, then run this:

```
from glob import glob
from json import loads
for path in glob('/Users/Jianhua/anaconda/conda-meta/*.json'):
    try:
        with open(path) as fh:
            data = fh.read()
            _ = loads(data)
    except:
        print(path)
        print(data)
        raise
```

What output does that give?
Hi, Kalefranz

Thank you for your response first. By your code, I got the output:
/Users/Jianhua/anaconda/conda-meta/mkl-2017.0.1-0.json
Well that's sure interesting. CC @msarahan. I wonder if this is related to some other recent issues... I can't see exactly how just yet though...
This blank conda-meta file... it's possible I have an off-by-one index error somewhere in the rollback code that could lead to this maybe...
@xfsm1912 Let's try this... 

    rm /Users/Jianhua/anaconda/conda-meta/mkl-2017.0.1-0.json

Then run that python code again.  Make sure it comes back without printing anything.  I mean, make sure nothing happens when you run that code snippet now.  Then

    conda install --no-deps mkl=2017

Let me know how that goes...
after delete the mkl-2017.0.1-0.json, nothing is output as running the previous code. But running install code, I got:

[Jianhua@d-172-26-7-254~]$ conda install --no-deps mkl=2017
Fetching package metadata .........
Solving package specifications: .

Package plan for installation in environment /Users/Jianhua/anaconda:

The following packages will be UPDATED:

    mkl: 11.3.3-0 --> 2017.0.1-0

Proceed ([y]/n)? y

ERROR conda.core.link:_execute_actions(318): An error occurred while installing package 'defaults::mkl-2017.0.1-0'.
LookupError('unknown encoding: ',)
Attempting to roll back.



LookupError('unknown encoding: ',)
Now I'm even more confused.  But I think we're making progress...

How about

    conda install --no-deps mkl=2017 -v -v -v

?  It might be a LOT of output.  That's ok.  Too much is better than not enough.
[Jianhua@d-172-26-7-254~]$ conda install --no-deps mkl=2017 -v -v -v
DEBUG conda.cli.main:_main(135): verbosity set to 3
An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

    https://github.com/conda/conda/issues



INFO conda.common.io:captured(59): overtaking stderr and stdout
INFO conda.common.io:captured(65): stderr and stdout yielding back
Current conda install:

               platform : osx-64
          conda version : 4.3.8
       conda is private : False
      conda-env version : 4.3.8
    conda-build version : 2.0.2
         python version : 2.7.12.final.0
       requests version : 2.12.4
       root environment : /Users/Jianhua/anaconda  (writable)
    default environment : /Users/Jianhua/anaconda
       envs directories : /Users/Jianhua/anaconda/envs
          package cache : /Users/Jianhua/anaconda/pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/osx-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/osx-64
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/osx-64
                          https://repo.continuum.io/pkgs/pro/noarch
            config file : None
           offline mode : False
             user-agent : conda/4.3.8 requests/2.12.4 CPython/2.7.12 Darwin/16.4.0 OSX/10.12.3
                UID:GID : 501:20



`$ /Users/Jianhua/anaconda/bin/conda install --no-deps mkl=2017 -v -v -v`




    Traceback (most recent call last):
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/exceptions.py", line 617, in conda_exception_handler
        return_value = func(*args, **kwargs)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 137, in _main
        exit_code = args.func(args, p)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main_install.py", line 80, in execute
        install(args, parser, 'install')
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 136, in install
        linked_dists = install_linked(prefix)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 121, in linked
        return set(linked_data(prefix, ignore_channels=ignore_channels).keys())
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 113, in linked_data
        load_linked_data(prefix, dist_name, ignore_channels=ignore_channels)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 34, in load_linked_data
        rec = json.load(fi)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/__init__.py", line 291, in load
        **kw)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/decoder.py", line 364, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Users/Jianhua/anaconda/lib/python2.7/json/decoder.py", line 382, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

No JSON again

Oh even better!  One more time

    rm /Users/Jianhua/anaconda/conda-meta/mkl-2017.0.1-0.json

and then now 

    conda install --no-deps mkl=2017 -v -v -v
As I type conda install --no-deps mkl=2017 -v -v -v, output is a lot, but finally:
An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

    https://github.com/conda/conda/issues



INFO conda.common.io:captured(59): overtaking stderr and stdout
INFO conda.common.io:captured(65): stderr and stdout yielding back
Current conda install:

               platform : osx-64
          conda version : 4.3.8
       conda is private : False
      conda-env version : 4.3.8
    conda-build version : 2.0.2
         python version : 2.7.12.final.0
       requests version : 2.12.4
       root environment : /Users/Jianhua/anaconda  (writable)
    default environment : /Users/Jianhua/anaconda
       envs directories : /Users/Jianhua/anaconda/envs
          package cache : /Users/Jianhua/anaconda/pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/osx-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/osx-64
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/osx-64
                          https://repo.continuum.io/pkgs/pro/noarch
            config file : None
           offline mode : False
             user-agent : conda/4.3.8 requests/2.12.4 CPython/2.7.12 Darwin/16.4.0 OSX/10.12.3
                UID:GID : 501:20



`$ /Users/Jianhua/anaconda/bin/conda install --no-deps mkl=2017 -v -v -v`




    Traceback (most recent call last):
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/exceptions.py", line 617, in conda_exception_handler
        return_value = func(*args, **kwargs)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 137, in _main
        exit_code = args.func(args, p)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main_install.py", line 80, in execute
        install(args, parser, 'install')
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 347, in install
        execute_actions(actions, index, verbose=not context.quiet)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/plan.py", line 837, in execute_actions
        execute_instructions(plan, index, verbose)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/instructions.py", line 258, in execute_instructions
        cmd(state, arg)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/instructions.py", line 119, in UNLINKLINKTRANSACTION_CMD
        txn.execute()
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/link.py", line 277, in execute
        rollback_excs,
    CondaMultiError: unknown encoding: 
Unfortunately that doesn't quite help.  I actually need the full output.  You can email it to me in a file if you'd like.  kale@franz.io
Open python, then run this:

```
from glob import glob
from json import loads
for path in glob('/Users/Jianhua/anaconda/conda-meta/*.json'):
    try:
        with open(path) as fh:
            data = fh.read()
            _ = loads(data)
    except:
        print(path)
        print(data)
        raise
```

What output does that give?
Hi, Kalefranz

Thank you for your response first. By your code, I got the output:
/Users/Jianhua/anaconda/conda-meta/mkl-2017.0.1-0.json
Well that's sure interesting. CC @msarahan. I wonder if this is related to some other recent issues... I can't see exactly how just yet though...
This blank conda-meta file... it's possible I have an off-by-one index error somewhere in the rollback code that could lead to this maybe...
@xfsm1912 Let's try this... 

    rm /Users/Jianhua/anaconda/conda-meta/mkl-2017.0.1-0.json

Then run that python code again.  Make sure it comes back without printing anything.  I mean, make sure nothing happens when you run that code snippet now.  Then

    conda install --no-deps mkl=2017

Let me know how that goes...
after delete the mkl-2017.0.1-0.json, nothing is output as running the previous code. But running install code, I got:

[Jianhua@d-172-26-7-254~]$ conda install --no-deps mkl=2017
Fetching package metadata .........
Solving package specifications: .

Package plan for installation in environment /Users/Jianhua/anaconda:

The following packages will be UPDATED:

    mkl: 11.3.3-0 --> 2017.0.1-0

Proceed ([y]/n)? y

ERROR conda.core.link:_execute_actions(318): An error occurred while installing package 'defaults::mkl-2017.0.1-0'.
LookupError('unknown encoding: ',)
Attempting to roll back.



LookupError('unknown encoding: ',)
Now I'm even more confused.  But I think we're making progress...

How about

    conda install --no-deps mkl=2017 -v -v -v

?  It might be a LOT of output.  That's ok.  Too much is better than not enough.
[Jianhua@d-172-26-7-254~]$ conda install --no-deps mkl=2017 -v -v -v
DEBUG conda.cli.main:_main(135): verbosity set to 3
An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

    https://github.com/conda/conda/issues



INFO conda.common.io:captured(59): overtaking stderr and stdout
INFO conda.common.io:captured(65): stderr and stdout yielding back
Current conda install:

               platform : osx-64
          conda version : 4.3.8
       conda is private : False
      conda-env version : 4.3.8
    conda-build version : 2.0.2
         python version : 2.7.12.final.0
       requests version : 2.12.4
       root environment : /Users/Jianhua/anaconda  (writable)
    default environment : /Users/Jianhua/anaconda
       envs directories : /Users/Jianhua/anaconda/envs
          package cache : /Users/Jianhua/anaconda/pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/osx-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/osx-64
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/osx-64
                          https://repo.continuum.io/pkgs/pro/noarch
            config file : None
           offline mode : False
             user-agent : conda/4.3.8 requests/2.12.4 CPython/2.7.12 Darwin/16.4.0 OSX/10.12.3
                UID:GID : 501:20



`$ /Users/Jianhua/anaconda/bin/conda install --no-deps mkl=2017 -v -v -v`




    Traceback (most recent call last):
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/exceptions.py", line 617, in conda_exception_handler
        return_value = func(*args, **kwargs)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 137, in _main
        exit_code = args.func(args, p)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main_install.py", line 80, in execute
        install(args, parser, 'install')
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 136, in install
        linked_dists = install_linked(prefix)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 121, in linked
        return set(linked_data(prefix, ignore_channels=ignore_channels).keys())
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 113, in linked_data
        load_linked_data(prefix, dist_name, ignore_channels=ignore_channels)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/linked_data.py", line 34, in load_linked_data
        rec = json.load(fi)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/__init__.py", line 291, in load
        **kw)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/Users/Jianhua/anaconda/lib/python2.7/json/decoder.py", line 364, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Users/Jianhua/anaconda/lib/python2.7/json/decoder.py", line 382, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

No JSON again

Oh even better!  One more time

    rm /Users/Jianhua/anaconda/conda-meta/mkl-2017.0.1-0.json

and then now 

    conda install --no-deps mkl=2017 -v -v -v
As I type conda install --no-deps mkl=2017 -v -v -v, output is a lot, but finally:
An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

    https://github.com/conda/conda/issues



INFO conda.common.io:captured(59): overtaking stderr and stdout
INFO conda.common.io:captured(65): stderr and stdout yielding back
Current conda install:

               platform : osx-64
          conda version : 4.3.8
       conda is private : False
      conda-env version : 4.3.8
    conda-build version : 2.0.2
         python version : 2.7.12.final.0
       requests version : 2.12.4
       root environment : /Users/Jianhua/anaconda  (writable)
    default environment : /Users/Jianhua/anaconda
       envs directories : /Users/Jianhua/anaconda/envs
          package cache : /Users/Jianhua/anaconda/pkgs
           channel URLs : https://repo.continuum.io/pkgs/free/osx-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/r/osx-64
                          https://repo.continuum.io/pkgs/r/noarch
                          https://repo.continuum.io/pkgs/pro/osx-64
                          https://repo.continuum.io/pkgs/pro/noarch
            config file : None
           offline mode : False
             user-agent : conda/4.3.8 requests/2.12.4 CPython/2.7.12 Darwin/16.4.0 OSX/10.12.3
                UID:GID : 501:20



`$ /Users/Jianhua/anaconda/bin/conda install --no-deps mkl=2017 -v -v -v`




    Traceback (most recent call last):
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/exceptions.py", line 617, in conda_exception_handler
        return_value = func(*args, **kwargs)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 137, in _main
        exit_code = args.func(args, p)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/main_install.py", line 80, in execute
        install(args, parser, 'install')
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 347, in install
        execute_actions(actions, index, verbose=not context.quiet)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/plan.py", line 837, in execute_actions
        execute_instructions(plan, index, verbose)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/instructions.py", line 258, in execute_instructions
        cmd(state, arg)
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/instructions.py", line 119, in UNLINKLINKTRANSACTION_CMD
        txn.execute()
      File "/Users/Jianhua/anaconda/lib/python2.7/site-packages/conda/core/link.py", line 277, in execute
        rollback_excs,
    CondaMultiError: unknown encoding: 
Unfortunately that doesn't quite help.  I actually need the full output.  You can email it to me in a file if you'd like.  kale@franz.io

Resolving patch:
diff --git a/conda/core/path_actions.py b/conda/core/path_actions.py
--- a/conda/core/path_actions.py
+++ b/conda/core/path_actions.py
@@ -510,15 +510,11 @@ def __init__(self, transaction_context, package_info, target_prefix, target_shor
                                                               None, None, target_prefix,
                                                               target_short_path)
         self.linked_package_record = linked_package_record
-        self._record_written_to_disk = False
         self._linked_data_loaded = False
 
     def execute(self):
         log.trace("creating linked package record %s", self.target_full_path)
-
         write_linked_package_record(self.target_prefix, self.linked_package_record)
-        self._record_written_to_disk = True
-
         load_linked_data(self.target_prefix, Dist(self.package_info.repodata_record).dist_name,
                          self.linked_package_record)
         self._linked_data_loaded = True
@@ -528,10 +524,7 @@ def reverse(self):
         if self._linked_data_loaded:
             delete_linked_data(self.target_prefix, Dist(self.package_info.repodata_record),
                                delete=False)
-        if self._record_written_to_disk:
-            rm_rf(self.target_full_path)
-        else:
-            log.trace("record was not created %s", self.target_full_path)
+        rm_rf(self.target_full_path)
 
 
 class CreatePrivateEnvMetaAction(CreateInPrefixPathAction):
diff --git a/conda/gateways/disk/create.py b/conda/gateways/disk/create.py
--- a/conda/gateways/disk/create.py
+++ b/conda/gateways/disk/create.py
@@ -22,7 +22,7 @@
 from ..._vendor.auxlib.entity import EntityEncoder
 from ..._vendor.auxlib.ish import dals
 from ...base.context import context
-from ...common.compat import on_win
+from ...common.compat import on_win, ensure_binary
 from ...common.path import win_path_ok
 from ...exceptions import BasicClobberError, CondaOSError, maybe_raise
 from ...models.dist import Dist
@@ -49,6 +49,14 @@
 """)
 
 
+def write_as_json_to_file(file_path, obj):
+    log.trace("writing json to file %s", file_path)
+    with open(file_path, str('wb')) as fo:
+        json_str = json.dumps(obj, indent=2, sort_keys=True, separators=(',', ': '),
+                              cls=EntityEncoder)
+        fo.write(ensure_binary(json_str))
+
+
 def create_unix_python_entry_point(target_full_path, python_full_path, module, func):
     if lexists(target_full_path):
         maybe_raise(BasicClobberError(
@@ -58,7 +66,7 @@ def create_unix_python_entry_point(target_full_path, python_full_path, module, f
         ), context)
 
     pyscript = python_entry_point_template % {'module': module, 'func': func}
-    with open(target_full_path, 'w') as fo:
+    with open(target_full_path, str('w')) as fo:
         fo.write('#!%s\n' % python_full_path)
         fo.write(pyscript)
     make_executable(target_full_path)
@@ -75,7 +83,7 @@ def create_windows_python_entry_point(target_full_path, module, func):
         ), context)
 
     pyscript = python_entry_point_template % {'module': module, 'func': func}
-    with open(target_full_path, 'w') as fo:
+    with open(target_full_path, str('w')) as fo:
         fo.write(pyscript)
 
     return target_full_path
@@ -113,11 +121,7 @@ def write_linked_package_record(prefix, record):
             target_path=conda_meta_full_path,
             context=context,
         ), context)
-    with open(conda_meta_full_path, 'w') as fo:
-        json_str = json.dumps(record, indent=2, sort_keys=True, cls=EntityEncoder)
-        if hasattr(json_str, 'decode'):
-            json_str = json_str.decode('utf-8')
-        fo.write(json_str)
+    write_as_json_to_file(conda_meta_full_path, record)
 
 
 def make_menu(prefix, file_path, remove=False):
@@ -287,8 +291,7 @@ def create_private_envs_meta(pkg, root_prefix, private_env_prefix):
 
     private_envs_json = get_json_content(context.private_envs_json_path)
     private_envs_json[pkg] = private_env_prefix
-    with open(context.private_envs_json_path, "w") as f:
-        json.dump(private_envs_json, f)
+    write_as_json_to_file(context.private_envs_json_path, private_envs_json)
 
 
 def create_private_pkg_entry_point(source_full_path, target_full_path, python_full_path):
@@ -300,7 +303,7 @@ def create_private_pkg_entry_point(source_full_path, target_full_path, python_fu
         ), context)
 
     entry_point = application_entry_point_template % {"source_full_path": source_full_path}
-    with open(target_full_path, "w") as fo:
+    with open(target_full_path, str("w")) as fo:
         fo.write('#!%s\n' % python_full_path)
         fo.write(entry_point)
     make_executable(target_full_path)


Test patch: