GitBash中的virtualenvwrapper错误
我在Windows 7的GitBash里尝试设置virtualenvwrapper。我写了以下几行命令:
1 $ export WORKON_HOME=$HOME/.virtualenvs
2 $ export MSYS_HOME=/c/msys/1.0
3 $ source /usr/local/bin/virtualenvwrapper.sh
但是最后一行出现了错误:
source /usr/local/bin/virtualenvwrapper.sh
sh.exe: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
我找到我电脑上virtualenvwrapper.sh
的实际位置,并且修改了目录名称。在我的电脑上,它的路径是/c/Python27/Scripts/virtualenvwrapper.sh
。当我再次运行命令:
$source /c/Python27/Scripts/virtualenvwrapper.sh
我收到了下一个错误信息:
sh.exe":mktemp:command not found ERROR: virtualenvwrapper could not create a temporary file name
我检查了我的环境变量:C:\python27\;C:\python27\scripts\;C:\python27\scripts\virtualenvwrapper.sh\;C:\msys;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\Git\bin\
我不知道我哪里出错了。
2 个回答
我在一台使用Windows 8的电脑上找到了这个问题的解决办法,使用的是GitBash。
简而言之:
下载mktemp这个工具,把它放在GitBash能找到的地方,然后编辑一下virtualenvwrapper.sh文件,在第202行加上一个touch命令,创建一个文件。看起来应该是这样的:
file="$(virtualenvwrapper_mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX)"
touch $file # this is the new line
if [ $? -ne 0 ] || [ -z "$file" ] || [ ! -f "$file" ]
详细解答:
正如khampson提到的,你需要下载mktemp,并把它放在Git\bin目录下(通常是C:\Program Files (x86)\Git\bin)。之后,运行virtualenvwrapper.sh文件时会出现一个错误,提示:
path = C:/Users/User/AppData/Local/Temp/virtualenvwrapper-initialize-hook-XXXXXX XXXX
lpPathBuffer = C:\Users\User\AppData\Local\Temp\
szTempName = C:\Users\User\AppData\Local\Temp\tmp23A9.tmp
path = C:\Users\User\AppData\Local\Temp\tmp23A9.tmp
fd = 3
ERROR: virtualenvwrapper could not create a temporary file name.
在第202行(查看源代码),你会看到一个调用virtualenvwrapper_mktemp的函数(这个函数其实就是用来调用mktemp的),本来是用来创建新的临时文件的,但在Windows上似乎不太管用。
在mktemp的手册中,示例部分总是会向新文件句柄发送一些内容,这样就能强制创建文件。
所以,不要像手册那样用echo发送一个空字符串,而是直接在virtualenvwrapper.sh中加上一个touch命令:
file="$(virtualenvwrapper_mktemp -t virtualenvwrapper-$suffix-XXXXXXXXXX)"
touch $file # new command here
这样应该能强制Windows创建临时文件。我不能发更多链接因为我的声望不够,但希望这能帮到某些人。
更新
我在virtualenvwrapper的代码库上创建了一个拉取请求,并且得到了批准。你可以在这里看到我建议添加的touch命令。
这个错误的意思是说,sh.exe(也就是命令行工具)找不到一个叫做mktemp的命令,这说明在你的GitBash环境中没有这个命令。
一种解决办法是,你可以下载一个适用于Windows的mktemp版本,比如可以去这个链接:http://gnuwin32.sourceforge.net/packages/mktemp.htm,然后把它放到C:\Program Files (x86)\Git\bin这个文件夹里。这样一来,命令行工具就能找到mktemp这个命令了,接下来就可以继续操作了。