# Buggy code
def execute(args, parser):
    if not args.name:
        # Note, this is a hack fofr get_prefix that assumes argparse results
        # TODO Refactor common.get_prefix
        name = os.environ.get("CONDA_DEFAULT_ENV", False)
        if not name:
            msg = "Unable to determine environment\n\n"
            msg += textwrap.dedent("""
                Please re-run this command with one of the following options:

                * Provide an environment name via --name or -n
                * Re-run this command inside an activated conda environment.""").lstrip()
            # TODO Add json support
            error_and_exit(msg, json=False)
        args.name = name
    else:
        name = args.name
    prefix = get_prefix(args)
    env = from_environment(
        name, prefix, no_builds=args.no_builds, ignore_channels=args.ignore_prefix
    )

    if args.override_channels:
        env.remove_channels()

    if args.channel is not None:
        env.add_channels(args.channel)

    if args.file is None:
        print(env.to_yaml())
    else:
        fp = open(args.file, "wb")
        env.to_yaml(stream=fp)


# Traceback (AttributeError)
n 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 : linux-64
conda version : 4.2.1
conda is private : False
conda-env version : 2.5.2
conda-build version : 1.21.11+0.g5b44ab3.dirty
python version : 2.7.12.final.0
requests version : 2.10.0
root environment : /reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7  (writable)
default environment : /reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7/envs/anabase
envs directories : /reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7/envs
package cache : /reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7/pkgs
channel URLs : file:///reg/g/psdm/sw/conda/channels/system-rhel7
file:///reg/g/psdm/sw/conda/channels/psana-rhel7
file:///reg/g/psdm/sw/conda/channels/external-rhel7
defaults
scikit-beam
file:///reg/g/psdm/sw/conda/channels/testing-rhel7
config file : /reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7/.condarc
offline mode : False


`$ /reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7/bin/conda-env export`


Traceback (most recent call last):
File "/reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7/lib/python2.7/site-packages/conda/exceptions.py", line 403, in conda_exception_handler
return_value = func(*args, **kwargs)
File "/reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7/lib/python2.7/site-packages/conda_env/cli/main_export.py", line 93, in execute
ignore_channels=args.ignore_prefix)
AttributeError: 'Namespace' object has no attribute 'ignore_prefix'
../manage/config/environment-anabase-1.0.0.yml (END)

# Fixed code
def execute(args, parser):
    if not args.name:
        # Note, this is a hack fofr get_prefix that assumes argparse results
        # TODO Refactor common.get_prefix
        name = os.environ.get("CONDA_DEFAULT_ENV", False)
        if not name:
            msg = "Unable to determine environment\n\n"
            msg += textwrap.dedent("""
                Please re-run this command with one of the following options:

                * Provide an environment name via --name or -n
                * Re-run this command inside an activated conda environment.""").lstrip()
            # TODO Add json support
            error_and_exit(msg, json=False)
        args.name = name
    else:
        name = args.name
    prefix = get_prefix(args)
    env = from_environment(
        name, prefix, no_builds=args.no_builds, ignore_channels=args.ignore_channels
    )

    if args.override_channels:
        env.remove_channels()

    if args.channel is not None:
        env.add_channels(args.channel)

    if args.file is None:
        print(env.to_yaml())
    else:
        fp = open(args.file, "wb")
        env.to_yaml(stream=fp)