python匹配可执行文件中的Keyerror

python匹配可执行文件中的Keyerror,python,Python,我要走了 Traceback (most recent call last): File "C:\Downloaders\nzbToMedia\TorrentToMedia.py", line 323, in <module> state = unpack(dirpath, file, outputDestination) File "C:\Downloaders\nzbToMedia\TorrentToMedia.py", line 121, in unpack

我要走了

Traceback (most recent call last):   File
"C:\Downloaders\nzbToMedia\TorrentToMedia.py", line 323, in <module>
    state = unpack(dirpath, file, outputDestination)
File "C:\Downloaders\nzbToMedia\TorrentToMedia.py", line 121, in unpack
    if which(win_7z_exe):   
File "C:\Downloaders\nzbToMedia\TorrentToMedia.py", line 35, in which
    for path in os.environ["PATH"].split(os.pathsep):   
File "C:\Python27\lib\os.py", line 423, in __getitem__
    return self.data[key.upper()] 
KeyError: 'PATH'
代码调用which()


在我看来,您没有设置
路径
环境变量…但我有,我可以手动查看它们。我不应该申报比我已经申报的更多,或者?你能手动看到什么?在调用
哪个
之前,您是否尝试过
print os.environ[“PATH”]
print sorted(os.environ.keys())
,因此,如果在
PATH
上出现
KeyError
,这里没有什么棘手的问题,这意味着在进程启动时读取环境时没有环境变量
PATH
。好吧,那么运行2.7就没有办法了?我做了一个打印,它显示了相当多的信息(我正在寻找)
def which(program):
    import os
    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            path = path.strip('"')
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None
if os.name == 'nt':
        win_7z_exes = ['7z.exe', 'C:\\Program Files\\7-Zip\\7z.exe', 'C:\\Program Files (x86)\\7-Zip\\7z.exe',]
        switch_7z = "x -y"
        exts_7z = [".rar", ".zip", ".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tbz", ".tar.lzma", ".tlz", ".tar.xz", ".txz", ".7z", ".xz", ".lzma",]
        for win_7z_exe in win_7z_exes:
            if which(win_7z_exe):
                EXTRACT_COMMANDS = dict.fromkeys(exts_7z, [win_7z_exe, switch_7z])
                break

    # Using Linux
    elif os.name == 'posix':
        Logger.info("EXTRACTOR: We are using *nix")
        required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] # Need to add a check for which commands that can be utilized in *nix systems
        EXTRACT_COMMANDS = {
        ".rar": ["unrar", "x -o+ -y"],
        ".zip": ["unzip", ""],
        ".tar.gz": ["tar", "xzf"],
        ".tgz": ["tar", "xzf"],
        ".tar.bz2": ["tar", "xjf"],
        ".tbz": ["tar", "xjf"],
        ".tar.lzma": ["tar", "--lzma xf"],
        ".tlz": ["tar", "--lzma xf"],
        ".txz": ["tar", "--xz xf"],
        ".7z": ["7zr", "x"],
        }
        for cmd in required_cmds:
            if not which(cmd):
                for k,v in EXTRACT_COMMANDS.items():
                    if cmd in v[0]:
                        Logger.debug("EXTRACTOR: Command %s not found, disabling support for %s", cmd, k)
                        del EXTRACT_COMMANDS[k]
    else:
        Logger.error("EXTRACTOR: Cant determine host OS while extracting, Exiting")