# Buggy code
def build_deactivate(self):
    # query environment
    old_conda_shlvl = int(os.getenv("CONDA_SHLVL", 0))
    if old_conda_shlvl <= 0:
        return {
            "unset_vars": (),
            "set_vars": {},
            "export_vars": {},
            "deactivate_scripts": (),
            "activate_scripts": (),
        }
    old_conda_prefix = os.environ["CONDA_PREFIX"]
    deactivate_scripts = self._get_deactivate_scripts(old_conda_prefix)

    new_conda_shlvl = old_conda_shlvl - 1
    new_path = self.pathsep_join(self._remove_prefix_from_path(old_conda_prefix))

    assert old_conda_shlvl > 0
    set_vars = {}
    if old_conda_shlvl == 1:
        # TODO: warn conda floor
        conda_prompt_modifier = ""
        unset_vars = (
            "CONDA_PREFIX",
            "CONDA_DEFAULT_ENV",
            "CONDA_PYTHON_EXE",
            "CONDA_PROMPT_MODIFIER",
        )
        export_vars = {
            "PATH": new_path,
            "CONDA_SHLVL": new_conda_shlvl,
        }
        activate_scripts = ()
    else:
        new_prefix = os.getenv("CONDA_PREFIX_%d" % new_conda_shlvl)
        conda_default_env = self._default_env(new_prefix)
        conda_prompt_modifier = self._prompt_modifier(conda_default_env)

        unset_vars = ("CONDA_PREFIX_%d" % new_conda_shlvl,)
        export_vars = {
            "PATH": new_path,
            "CONDA_SHLVL": new_conda_shlvl,
            "CONDA_PREFIX": new_prefix,
            "CONDA_DEFAULT_ENV": conda_default_env,
            "CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
        }
        activate_scripts = self._get_activate_scripts(new_prefix)

    self._update_prompt(set_vars, conda_prompt_modifier)

    return {
        "unset_vars": unset_vars,
        "set_vars": set_vars,
        "export_vars": export_vars,
        "deactivate_scripts": deactivate_scripts,
        "activate_scripts": activate_scripts,
    }


# Traceback (KeyError)
error: KeyError(u'CONDA_PREFIX',)
command: D:\Anaconda2\Scripts\conda shell.cmd.exe deactivate deactivate
user_agent: conda/4.4.4 requests/2.18.4 CPython/2.7.14 Windows/10 Windows/10.0.16299
_messageid: -9223372036842425555
_messagetime: 1514685063692 / 2017-12-30 19:51:03

Traceback (most recent call last):
File "D:\Anaconda2\lib\site-packages\conda\cli\main.py", line 111, in main
return activator_main()
File "D:\Anaconda2\lib\site-packages\conda\activate.py", line 529, in main
sys.stdout.write(activator.execute())
File "D:\Anaconda2\lib\site-packages\conda\activate.py", line 149, in execute
return getattr(self, self.command)()
File "D:\Anaconda2\lib\site-packages\conda\activate.py", line 139, in deactivate
return self._finalize(self._yield_commands(self.build_deactivate()),
File "D:\Anaconda2\lib\site-packages\conda\activate.py", line 288, in build_deactivate
old_conda_prefix = os.environ['CONDA_PREFIX']
File "D:\Anaconda2\lib\os.py", line 425, in __getitem__
return self.data[key.upper()]
KeyError: u'CONDA_PREFIX'

# Fixed code
def build_deactivate(self):
    # query environment
    old_conda_shlvl = int(os.getenv("CONDA_SHLVL", 0))
    old_conda_prefix = os.getenv("CONDA_PREFIX", None)
    if old_conda_shlvl <= 0 or old_conda_prefix is None:
        return {
            "unset_vars": (),
            "set_vars": {},
            "export_vars": {},
            "deactivate_scripts": (),
            "activate_scripts": (),
        }
    deactivate_scripts = self._get_deactivate_scripts(old_conda_prefix)

    new_conda_shlvl = old_conda_shlvl - 1
    new_path = self.pathsep_join(self._remove_prefix_from_path(old_conda_prefix))

    assert old_conda_shlvl > 0
    set_vars = {}
    if old_conda_shlvl == 1:
        # TODO: warn conda floor
        conda_prompt_modifier = ""
        unset_vars = (
            "CONDA_PREFIX",
            "CONDA_DEFAULT_ENV",
            "CONDA_PYTHON_EXE",
            "CONDA_PROMPT_MODIFIER",
        )
        export_vars = {
            "PATH": new_path,
            "CONDA_SHLVL": new_conda_shlvl,
        }
        activate_scripts = ()
    else:
        new_prefix = os.getenv("CONDA_PREFIX_%d" % new_conda_shlvl)
        conda_default_env = self._default_env(new_prefix)
        conda_prompt_modifier = self._prompt_modifier(conda_default_env)

        unset_vars = ("CONDA_PREFIX_%d" % new_conda_shlvl,)
        export_vars = {
            "PATH": new_path,
            "CONDA_SHLVL": new_conda_shlvl,
            "CONDA_PREFIX": new_prefix,
            "CONDA_DEFAULT_ENV": conda_default_env,
            "CONDA_PROMPT_MODIFIER": conda_prompt_modifier,
        }
        activate_scripts = self._get_activate_scripts(new_prefix)

    self._update_prompt(set_vars, conda_prompt_modifier)

    return {
        "unset_vars": unset_vars,
        "set_vars": set_vars,
        "export_vars": export_vars,
        "deactivate_scripts": deactivate_scripts,
        "activate_scripts": activate_scripts,
    }