2013-03-12 57 views
2

當我運行vi --version時,我看到VIM - Vi IMproved 7.3,但是當我運行以下腳本時,它會打印出版本7.2。爲什麼?爲什麼subprocess.Popen使用vim的舊版本?

pathnameviwhich vi打印/usr/local/bin/vim--version7.3which gvim打印/usr/bin/gvim,並且--version也打印更新版本的vim。

echo $EDITOR打印vi

#!/usr/bin/python 
import os 
import sys 
import os.path 
import subprocess 
import tempfile 


def exec_vimcmd(commands, pathname='', error_stream=None): 
"""Run a list of Vim 'commands' and return the commands output.""" 
try: 
    perror = error_stream.write 
except AttributeError: 
    perror = sys.stderr.write 

if not pathname: 
    pathname = os.environ.get('EDITOR', 'gvim') 

args = [pathname, '-u', 'NONE', '-esX', '-c', 'set cpo&vim'] 
fd, tmpname = tempfile.mkstemp(prefix='runvimcmd', suffix='.clewn') 
commands.insert(0, 'redir! >%s' % tmpname) 
commands.append('quit') 
for cmd in commands: 
    args.extend(['-c', cmd]) 

output = f = None 
try: 
    try: 
     print "args are" 
     print args 
     subprocess.Popen(args).wait() 
     f = os.fdopen(fd) 
     output = f.read() 
     print "output is" 
     print output 
     print "that's the end of the output" 
    except (OSError, IOError), err: 
     if isinstance(err, OSError) and err.errno == errno.ENOENT: 
      perror("Failed to run '%s' as Vim.\n" % args[0]) 
      perror("Please set the EDITOR environment variable or run " 
          "'pyclewn --editor=/path/to/(g)vim'.\n\n") 
     else: 
      perror("Failed to run Vim as:\n'%s'\n\n" % str(args)) 
      perror("Error; %s\n", err) 
     raise 
finally: 
    if f is not None: 
     f.close() 

exec_vimcmd(['version']) 

打印的ARGS是

['vi', '-u', 'NONE', '-esX', '-c', 'set cpo&vim', '-c', 'redir! >/var/folders/86/062qtcyx2rxbnmn8mtpkyghs0r0r_z/T/runvimcmducLQCe.clewn', '-c', 'version', '-c', 'quit'] 
+2

'print args'的輸出是什麼? – 2013-03-12 15:16:31

回答

1

找出值被分配給pathname,看它是否與在命令提示輸入which vimwhich gvim一致。您的腳本正在查看您的$EDITOR環境變量,但是當您從命令行運行(g)vim時,它將搜索您的$PATH以查找第一個匹配項。例如,您可能正在從CLI運行/opt/local/bin/vim,但從腳本運行/usr/bin/vim

+0

這不回答我的問題。路徑名是vi。其中vi是/ usr/local/bin/vim。其中givm是/ usr/bin/gvim。 $ EDITOR是vi。 – 2013-03-12 21:14:15

+1

在你的路徑中是否還有其他(g)vi(m)二進制文件,或許是'/ usr/bin/vi'或類似的東西?不知何故,Python正在使用與你的shell不同的搜索路徑。如果將'$ EDITOR'設置爲'/ usr/local/bin/vim'會發生什麼? – MattDMo 2013-03-12 21:43:21

相關問題