# Buggy code
def generate_config_py(target):
    """Generate config.py file containing system_info information
    used during building the package.

    Usage:
        config['py_modules'].append((packagename, '__config__',generate_config_py))
    """
    from numpy.distutils.system_info import system_info
    from distutils.dir_util import mkpath

    mkpath(os.path.dirname(target))
    f = open(target, "w")
    f.write(
        "# This file is generated by numpy's %s\n" % (os.path.basename(sys.argv[0]))
    )
    f.write("# It contains system_info results at the time of building this package.\n")
    f.write('__all__ = ["get_info","show"]\n\n')

    # For gfortran+msvc combination, extra shared libraries may exist
    f.write("""

import os
import sys

extra_dll_dir = os.path.join(os.path.dirname(__file__), '.libs')

if os.path.isdir(extra_dll_dir) and sys.platform == 'win32':
    try:
        from ctypes import windll, c_wchar_p
        _AddDllDirectory = windll.kernel32.AddDllDirectory
        _AddDllDirectory.argtypes = [c_wchar_p]
        # Needed to initialize AddDllDirectory modifications
        windll.kernel32.SetDefaultDllDirectories(0x1000)
    except AttributeError:
        def _AddDllDirectory(dll_directory):
            os.environ["PATH"] += os.pathsep + dll_directory

    _AddDllDirectory(extra_dll_dir)

""")

    for k, i in system_info.saved_results.items():
        f.write("%s=%r\n" % (k, i))
    f.write(r"""
def get_info(name):
    g = globals()
    return g.get(name, g.get(name + "_info", {}))

def show():
    for name,info_dict in globals().items():
        if name[0] == "_" or type(info_dict) is not type({}): continue
        print(name + ":")
        if not info_dict:
            print("  NOT AVAILABLE")
        for k,v in info_dict.items():
            v = str(v)
            if k == "sources" and len(v) > 200:
                v = v[:60] + " ...\n... " + v[-60:]
            print("    %s = %s" % (k,v))
    """)

    f.close()
    return target


# Traceback (KeyError)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
six.reraise(*_exception)
File "/usr/local/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/code/analytics/models.py", line 6, in <module>
import pandas as pd
File "/usr/local/lib/python3.6/site-packages/pandas/__init__.py", line 13, in <module>
__import__(dependency)
File "/usr/local/lib/python3.6/site-packages/numpy/__init__.py", line 126, in <module>
from numpy.__config__ import show as show_config
File "/usr/local/lib/python3.6/site-packages/numpy/__config__.py", line 9, in <module>
os.environ["PATH"] += os.pathsep + extra_dll_dir
File "/usr/local/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'PATH'

# Fixed code
def generate_config_py(target):
    """Generate config.py file containing system_info information
    used during building the package.

    Usage:
        config['py_modules'].append((packagename, '__config__',generate_config_py))
    """
    from numpy.distutils.system_info import system_info
    from distutils.dir_util import mkpath

    mkpath(os.path.dirname(target))
    f = open(target, "w")
    f.write(
        "# This file is generated by numpy's %s\n" % (os.path.basename(sys.argv[0]))
    )
    f.write("# It contains system_info results at the time of building this package.\n")
    f.write('__all__ = ["get_info","show"]\n\n')

    # For gfortran+msvc combination, extra shared libraries may exist
    f.write("""

import os
import sys

extra_dll_dir = os.path.join(os.path.dirname(__file__), '.libs')

if os.path.isdir(extra_dll_dir) and sys.platform == 'win32':
    try:
        from ctypes import windll, c_wchar_p
        _AddDllDirectory = windll.kernel32.AddDllDirectory
        _AddDllDirectory.argtypes = [c_wchar_p]
        # Needed to initialize AddDllDirectory modifications
        windll.kernel32.SetDefaultDllDirectories(0x1000)
    except AttributeError:
        def _AddDllDirectory(dll_directory):
            os.environ.setdefault('PATH', '')
            os.environ['PATH'] += os.pathsep + dll_directory

    _AddDllDirectory(extra_dll_dir)

""")

    for k, i in system_info.saved_results.items():
        f.write("%s=%r\n" % (k, i))
    f.write(r"""
def get_info(name):
    g = globals()
    return g.get(name, g.get(name + "_info", {}))

def show():
    for name,info_dict in globals().items():
        if name[0] == "_" or type(info_dict) is not type({}): continue
        print(name + ":")
        if not info_dict:
            print("  NOT AVAILABLE")
        for k,v in info_dict.items():
            v = str(v)
            if k == "sources" and len(v) > 200:
                v = v[:60] + " ...\n... " + v[-60:]
            print("    %s = %s" % (k,v))
    """)

    f.close()
    return target