首页 > 解决方案 > Python 脚本在设置 tmp 文件夹时遇到问题

问题描述

尝试运行 Python 脚本(从大学网站下载)并不断收到错误,因为它显然无法设置临时目录。我在 Windows 10 下运行它并尝试过 Python 2.6 和 2.7。每当我运行代码时,我都会收到此错误:

Traceback (most recent call last):
  File "Calign.py", line 116, in <module>
    tmpbase = '/tmp/' + os.environ['USER'] + '_' + str(os.getpid())
  File "C:\Python27\lib\os.py", line 425, in __getitem__
    return self.data[key.upper()]
KeyError: 'USER

“Calign”Python 脚本中似乎有错误。最初我认为它只是翻转斜杠(使用 / ,其中 Windows 在路径中使用 \ ),但该修复只会产生更多错误。最终,任何修复脚本的尝试都不起作用——我的 Python 不够用,我不确定脚本在做什么。

如果有帮助,我对 python 脚本进行了详细的运行:

ente# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# C:\Python26\lib\site.pyc matches C:\Python26\lib\site.py
import site # precompiled from C:\Python26\lib\site.pyc
# C:\Python26\lib\os.pyc matches C:\Python26\lib\os.py
import os # precompiled from C:\Python26\lib\os.pyc
import errno # builtin
import nt # builtin
# C:\Python26\lib\ntpath.pyc matches C:\Python26\lib\ntpath.py
import ntpath # precompiled from C:\Python26\lib\ntpath.pyc
# C:\Python26\lib\stat.pyc matches C:\Python26\lib\stat.py
import stat # precompiled from C:\Python26\lib\stat.pyc
# C:\Python26\lib\genericpath.pyc matches C:\Python26\lib\genericpath.py
import genericpath # precompiled from C:\Python26\lib\genericpath.pyc
# C:\Python26\lib\warnings.pyc matches C:\Python26\lib\warnings.py
import warnings # precompiled from C:\Python26\lib\warnings.pyc
# C:\Python26\lib\linecache.pyc matches C:\Python26\lib\linecache.py
import linecache # precompiled from C:\Python26\lib\linecache.pyc
# C:\Python26\lib\types.pyc matches C:\Python26\lib\types.py
import types # precompiled from C:\Python26\lib\types.pyc
# C:\Python26\lib\UserDict.pyc matches C:\Python26\lib\UserDict.py
import UserDict # precompiled from C:\Python26\lib\UserDict.pyc
# C:\Python26\lib\_abcoll.pyc matches C:\Python26\lib\_abcoll.py
import _abcoll # precompiled from C:\Python26\lib\_abcoll.pyc
# C:\Python26\lib\abc.pyc matches C:\Python26\lib\abc.py
import abc # precompiled from C:\Python26\lib\abc.pyc
# C:\Python26\lib\copy_reg.pyc matches C:\Python26\lib\copy_reg.py
import copy_reg # precompiled from C:\Python26\lib\copy_reg.pyc
# C:\Python26\lib\locale.pyc matches C:\Python26\lib\locale.py
import locale # precompiled from C:\Python26\lib\locale.pyc
import encodings # directory C:\Python26\lib\encodings
# C:\Python26\lib\encodings\__init__.pyc matches C:\Python26\lib\encodings\__init__.py
import encodings # precompiled from C:\Python26\lib\encodings\__init__.pyc
# C:\Python26\lib\codecs.pyc matches C:\Python26\lib\codecs.py
import codecs # precompiled from C:\Python26\lib\codecs.pyc
import _codecs # builtin
# C:\Python26\lib\encodings\aliases.pyc matches C:\Python26\lib\encodings\aliases.py
import encodings.aliases # precompiled from C:\Python26\lib\encodings\aliases.pyc
# C:\Python26\lib\functools.pyc matches C:\Python26\lib\functools.py
import functools # precompiled from C:\Python26\lib\functools.pyc
import _functools # builtin
import _locale # builtin
# C:\Python26\lib\re.pyc matches C:\Python26\lib\re.py
import re # precompiled from C:\Python26\lib\re.pyc
# C:\Python26\lib\sre_compile.pyc matches C:\Python26\lib\sre_compile.py
import sre_compile # precompiled from C:\Python26\lib\sre_compile.pyc
import _sre # builtin
# C:\Python26\lib\sre_parse.pyc matches C:\Python26\lib\sre_parse.py
import sre_parse # precompiled from C:\Python26\lib\sre_parse.pyc
# C:\Python26\lib\sre_constants.pyc matches C:\Python26\lib\sre_constants.py
import sre_constants # precompiled from C:\Python26\lib\sre_constants.pyc
import operator # builtin
# C:\Python26\lib\encodings\cp1252.pyc matches C:\Python26\lib\encodings\cp1252.py
import encodings.cp1252 # precompiled from C:\Python26\lib\encodings\cp1252.pyc
Python 2.6 (r26:66721, Oct  2 2008, 11:06:43) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
# C:\Python26\lib\getopt.pyc matches C:\Python26\lib\getopt.py
import getopt # precompiled from C:\Python26\lib\getopt.pyc
# C:\Python26\lib\wave.pyc matches C:\Python26\lib\wave.py
import wave # precompiled from C:\Python26\lib\wave.pyc
# C:\Python26\lib\struct.pyc matches C:\Python26\lib\struct.py
import struct # precompiled from C:\Python26\lib\struct.pyc
import _struct # builtin
# C:\Python26\lib\chunk.pyc matches C:\Python26\lib\chunk.py
import chunk # precompiled from C:\Python26\lib\chunk.pyc
# C:\Python26\lib\io.pyc matches C:\Python26\lib\io.py
import io # precompiled from C:\Python26\lib\io.pyc
# C:\Python26\lib\__future__.pyc matches C:\Python26\lib\__future__.py
import __future__ # precompiled from C:\Python26\lib\__future__.pyc
import _fileio # builtin
# C:\Python26\lib\threading.pyc matches C:\Python26\lib\threading.py
import threading # precompiled from C:\Python26\lib\threading.pyc
import thread # builtin
import time # builtin
# C:\Python26\lib\traceback.pyc matches C:\Python26\lib\traceback.py
import traceback # precompiled from C:\Python26\lib\traceback.pyc
# C:\Python26\lib\collections.pyc matches C:\Python26\lib\collections.py
import collections # precompiled from C:\Python26\lib\collections.pyc
import _collections # builtin
# C:\Python26\lib\keyword.pyc matches C:\Python26\lib\keyword.py
import keyword # precompiled from C:\Python26\lib\keyword.pyc
import _bytesio # builtin
Traceback (most recent call last):
  File "Calign.py", line 116, in <module>
    tmpbase = '/tmp/' + os.environ['USER'] + '_' + str(os.getpid())
  File "C:\Python26\lib\os.py", line 423, in __getitem__
    return self.data[key.upper()]
KeyError: 'USER'
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.exitfunc
# clear sys.exc_type
# clear sys.exc_value
# clear sys.exc_traceback
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup __main__
# cleanup[1] __future__
# cleanup[1] chunk
# cleanup[1] locale
# cleanup[1] functools
# cleanup[1] encodings
# cleanup[1] site
# cleanup[1] operator
# cleanup[1] io
# cleanup[1] abc
# cleanup[1] threading
# cleanup[1] sre_constants
# cleanup[1] re
# cleanup[1] collections
# cleanup[1] _codecs
# cleanup[1] getopt
# cleanup[1] nt
# cleanup[1] _warnings
# cleanup[1] zipimport
# cleanup[1] encodings.cp1252
# cleanup[1] _collections
# cleanup[1] codecs
# cleanup[1] _bytesio
# cleanup[1] wave
# cleanup[1] _struct
# cleanup[1] _functools
# cleanup[1] _locale
# cleanup[1] keyword
# cleanup[1] thread
# cleanup[1] signal
# cleanup[1] traceback
# cleanup[1] encodings.aliases
# cleanup[1] time
# cleanup[1] exceptions
# cleanup[1] _fileio
# cleanup[1] sre_compile
# cleanup[1] _sre
# cleanup[1] struct
# cleanup[1] sre_parse
# cleanup[2] copy_reg
# cleanup[2] types
# cleanup[2] errno
# cleanup[2] _abcoll
# cleanup[2] ntpath
# cleanup[2] genericpath
# cleanup[2] stat
# cleanup[2] warnings
# cleanup[2] UserDict
# cleanup[2] os.path
# cleanup[2] linecache
# cleanup[2] os
# cleanup sys
# cleanup __builtin__
# cleanup ints: 26 unfreed ints
# cleanup floats: 2 unfreed floatsr code here

标签: pythonpython-2.7

解决方案


推荐阅读