# Buggy code
def from_environment(name, prefix, no_builds=False, ignore_channels=False):
    installed = install.linked(prefix, ignore_channels=ignore_channels)
    conda_pkgs = copy(installed)
    # json=True hides the output, data is added to installed
    add_pip_installed(prefix, installed, json=True)

    pip_pkgs = sorted(installed - conda_pkgs)

    if no_builds:
        dependencies = ["=".join(a.rsplit("-", 2)[0:2]) for a in sorted(conda_pkgs)]
    else:
        dependencies = ["=".join(a.rsplit("-", 2)) for a in sorted(conda_pkgs)]
    if len(pip_pkgs) > 0:
        dependencies.append(
            {"pip": ["==".join(a.rsplit("-", 2)[:2]) for a in pip_pkgs]}
        )
    # conda uses ruamel_yaml which returns a ruamel_yaml.comments.CommentedSeq
    # this doesn't dump correctly using pyyaml
    channels = context.channels

    return Environment(
        name=name, dependencies=dependencies, channels=channels, prefix=prefix
    )


# 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 from_environment(name, prefix, no_builds=False, ignore_channels=False):
    """
        Get environment object from prefix
    Args:
        name: The name of environment
        prefix: The path of prefix
        no_builds: Whether has build requirement
        ignore_channels: whether ingore_channels

    Returns:     Environment obejct

    """
    installed = install.linked(prefix, ignore_channels=ignore_channels)
    conda_pkgs = copy(installed)
    # json=True hides the output, data is added to installed
    add_pip_installed(prefix, installed, json=True)

    pip_pkgs = sorted(installed - conda_pkgs)

    if no_builds:
        dependencies = ["=".join(a.rsplit("-", 2)[0:2]) for a in sorted(conda_pkgs)]
    else:
        dependencies = ["=".join(a.rsplit("-", 2)) for a in sorted(conda_pkgs)]
    if len(pip_pkgs) > 0:
        dependencies.append(
            {"pip": ["==".join(a.rsplit("-", 2)[:2]) for a in pip_pkgs]}
        )
    # conda uses ruamel_yaml which returns a ruamel_yaml.comments.CommentedSeq
    # this doesn't dump correctly using pyyaml
    channels = context.channels

    return Environment(
        name=name, dependencies=dependencies, channels=channels, prefix=prefix
    )