# Buggy code
def print_explicit(prefix, add_md5=False):
    import json

    if not isdir(prefix):
        error_and_exit("Error: environment does not exist: %s" % prefix)
    print_export_header()
    print("@EXPLICIT")

    meta_dir = join(prefix, "conda-meta")
    for fn in sorted(os.listdir(meta_dir)):
        if not fn.endswith(".json"):
            continue
        with open(join(meta_dir, fn)) as fi:
            meta = json.load(fi)
        url = meta.get("url")

        def format_url():
            return "%s%s-%s-%s.tar.bz2" % (
                meta["channel"],
                meta["name"],
                meta["version"],
                meta["build"],
            )

        # two cases in which we want to try to format the url:
        # 1. There is no url key in the metadata
        # 2. The url key in the metadata is referencing a file on the local
        #    machine
        if not url:
            try:
                url = format_url()
            except KeyError:
                # Declare failure :-(
                print("# no URL for: %s" % fn[:-5])
                continue
        if url.startswith("file"):
            try:
                url = format_url()
            except KeyError:
                # declare failure and allow the url to be the file from which it was
                # originally installed
                continue
        md5 = meta.get("md5")
        print(url + ("#%s" % md5 if add_md5 and md5 else ""))


# Traceback (KeyError)
[root@42d0710d45b6 /]# conda index /opt/packages/linux-64/
updating index in: /opt/packages/linux-64
[root@42d0710d45b6 /]# ls /opt/packages/linux-64/
numpy-1.11.0-py35_1.tar.bz2  repodata.json  repodata.json.bz2
[root@42d0710d45b6 /]# conda install -c file:///opt/packages --yes numpy
Fetching package metadata .........
Solving package specifications: ..........

Package plan for installation in environment /opt/conda:

The following packages will be downloaded:

package                    |            build
---------------------------|-----------------
numpy-1.11.0               |           py35_1         6.1 MB  file:///opt/packages

The following NEW packages will be INSTALLED:

numpy: 1.11.0-py35_1 file:///opt/packages

Pruning fetched packages from the cache ...
Fetching packages ...
numpy-1.11.0-p 100% |#########################################################################################################| Time: 0:00:00 164.40 MB/s
Extracting packages ...
An unexpected error has occurred, please consider sending the                                                                                      |   0%
following traceback to the conda GitHub issue tracker at:

https://github.com/conda/conda/issues

Include the output of the command 'conda info' in your report.


Traceback (most recent call last):
File "/opt/conda/bin/conda", line 6, in <module>
sys.exit(main())
File "/opt/conda/lib/python3.5/site-packages/conda/cli/main.py", line 120, in main
args_func(args, p)
File "/opt/conda/lib/python3.5/site-packages/conda/cli/main.py", line 127, in args_func
args.func(args, p)
File "/opt/conda/lib/python3.5/site-packages/conda/cli/main_install.py", line 69, in execute
install(args, parser, 'install')
File "/opt/conda/lib/python3.5/site-packages/conda/cli/install.py", line 407, in install
execute_actions(actions, index, verbose=not args.quiet)
File "/opt/conda/lib/python3.5/site-packages/conda/plan.py", line 589, in execute_actions
inst.execute_instructions(plan, index, verbose)
File "/opt/conda/lib/python3.5/site-packages/conda/instructions.py", line 137, in execute_instructions
cmd(state, arg)
File "/opt/conda/lib/python3.5/site-packages/conda/instructions.py", line 60, in EXTRACT_CMD
extract(arg)
File "/opt/conda/lib/python3.5/site-packages/conda/install.py", line 833, in extract
rec = package_cache()[dist]
KeyError: 'file:///opt/packages::numpy-1.11.0-py35_1'

# Fixed code
def print_explicit(prefix, add_md5=False):
    if not isdir(prefix):
        error_and_exit("Error: environment does not exist: %s" % prefix)
    print_export_header()
    print("@EXPLICIT")
    for meta in sorted(linked_data(prefix).values(), key=lambda x: x["name"]):
        url = meta.get("url")
        if not url or url.startswith("<unknown>"):
            print("# no URL for: %s" % meta["fn"])
            continue
        md5 = meta.get("md5")
        print(url + ("#%s" % md5 if add_md5 and md5 else ""))