Skip to main content
 首页 » 编程设计

python之在 emacs 的 comint-mode 中使用参数执行 python 脚本

2024年11月01日8飞鱼

我正在为 python 脚本编写一个 comint 模式。

脚本可以启动:

/usr/bin/python3.3 tool-name arg0 

我正在使用 comint-mode,因为此调用会在运行前在提示中询问一些信息。

如果我创建:

(defun create-app () 
  "create application by using python tool" 
  (interactive) 
  (progn  
    (setq default-directory "/path/to/tool") 
    (setq buffer (get-buffer-create "*buffer_name*")) 
    (apply 'make-comint-in-buffer "tool" buffer "/usr/bin/python3.3" nil nil) 
    ) 
  ) 

一切正常,python 已启动,但如果我写:

(defun create-app () 
  "create application by using python tool" 
  (interactive) 
  (progn  
    (setq default-directory "/path/to/tool") 
    (setq buffer (get-buffer-create "*buffer_name*")) 
    (apply 'make-comint-in-buffer "tool" buffer "/usr/bin/python3.3 tool-name arg0" nil nil) 
    ) 
  ) 

缓冲区告诉我它不能执行程序“/usr/bin/python3.3 tool-name arg0”

有没有人知道(有或没有 comint)我如何启动这个 python 进程并让脚本在启动前询问一些信息?

请您参考如下方法:

make-comint-in-buffer 的文档字符串(C-hfmake-comint-in-bufferRET) 声明如下

 
 
(make-comint-in-buffer NAME BUFFER PROGRAM &optional STARTFILE &rest 
SWITCHES) 
 
Make a Comint process NAME in BUFFER, running PROGRAM. 
If BUFFER is nil, it defaults to NAME surrounded by `*'s. 
If there is a running process in BUFFER, it is not restarted. 
 
PROGRAM should be one of the following: 
- a string, denoting an executable program to create via 
  `start-file-process' 
- a cons pair of the form (HOST . SERVICE), denoting a TCP 
  connection to be opened via `open-network-stream' 
- nil, denoting a newly-allocated pty. 
 
... 
 
If PROGRAM is a string, any more args are arguments to PROGRAM. 

So the correct way to use the function is to have the only name of the program as a string and pass the arguments to be passed to the program, as additional arguments to make-comint-in-buffer as follows

(apply 'make-comint-in-buffer "tool" buffer "/usr/bin/python3.3" nil "tool-name" "arg0")