Python 在Cygwin中启动时,在virtualenvs中使用tempfile.mkdtemp()的奇怪行为

Python 在Cygwin中启动时,在virtualenvs中使用tempfile.mkdtemp()的奇怪行为,python,cygwin,virtualenv,Python,Cygwin,Virtualenv,在Python中创建临时目录时,我看到了不一致的行为: # System Python, Windows Console C:\Python33>python Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

在Python中创建临时目录时,我看到了不一致的行为:

# System Python, Windows Console

C:\Python33>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmpte7fcc'


# Virtualenv Python, Windows Console

Scripts>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmprziefb'

# System Python, Cygwin Console

$ /cygdrive/c/Python33/python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmprk4fcu'

# Virtualenv Python, Cygwin Console

$ Scripts/python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\cygwin\\tmp\\tmppeozcl
前三种情况在
C:\Windows\Temp
中创建临时目录(如预期)。为什么第四种情况会在其他地方创建临时目录


编辑:评论中要求的其他数据:

# System Python, Windows Console

C:\Python33>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\Python33']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP']


# Virtualenv Python, Windows Console

Scripts>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP']

# System Python, Cygwin Console

$ /cygdrive/c/Python33/python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\Cygwin\\tmp', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\Cygwin\\tmp', 'C:\\WINDOWS\\TEMP']

# Virtualenv Python, Cygwin Console

$ Scripts/python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\Cygwin\\tmp', 'C:\\Cygwin\\tmp', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\Cygwin\\tmp', 'C:\\Cygwin\\tmp']

tempfile模块查找环境变量TMPDIR、TEMP和TMP。如果设置了其中一个变量,则其值将用作临时文件和目录的基本目录。我猜Cygwin会将其中一个设置为C:\Cygwin\tmp。

知道为什么这只发生在virtualenv中吗?tempfile的输出是什么。_candidate_tempdir_list()和
[os.environ.get(envname)for envname in('TMPDIR','TEMP','tmp')]
对于每个变体,我都用您要求的数据更新了这个问题。显然,
virtualenv
环境也设置了
TMP
,这就是
mkdtemp()
所使用的?