Can';t从Clojure repl运行(sh“python hello”u world.py),但python3 hello”u world.py在shell中工作

Can';t从Clojure repl运行(sh“python hello”u world.py),但python3 hello”u world.py在shell中工作,shell,clojure,Shell,Clojure,我有一个简单的Python文件hello.py,其内容为:print(“hello World”),当我尝试使用(sh“python3 hello.py”)从REPL运行此文件时,我得到 Execution error (IOException) at java.lang.UNIXProcess/forkAndExec (UNIXProcess.java:-2). error=2, No such file or directory 显然,当我在shell中运行python3hello.py

我有一个简单的Python文件
hello.py
,其内容为:
print(“hello World”)
,当我尝试使用
(sh“python3 hello.py”)
从REPL运行此文件时,我得到

Execution error (IOException) at java.lang.UNIXProcess/forkAndExec (UNIXProcess.java:-2).
error=2, No such file or directory

显然,当我在shell中运行python3hello.py时,程序就会运行。如何从Clojure repl运行Python文件?

您可能对运行shell命令感兴趣

(shell-cmd <cmd-str>)
结果:

{:exit 0    ; unix exit status (0 -> normal) 
 :err ''    ; text from any errors 
 :out '...' ; text output as would printed to console 
}
首先尝试使用unix命令来帮助诊断任何路径问题


更新 查尔斯得到了正确的答案。当您通过
shell/sh
调用时,它希望每个参数使用不同的字符串。例如:

(ns tst.demo.core
(:使用tupelo.core tupelo.test)
(:需要
[tupelo.misc:作为misc]
[clojure.java.shell:as-shell]))
(宠爱
;`tupelo.misc/shell cmd`首先调用bash,所以空间是可以的
(is=(misc/shell cmd“python3 hello.py”)
{:退出0,:退出“Hello World\n”,:err”“})
;直接调用'python3'。需要单独的字符串“hello.py”
(is=(shell/sh“python3”“hello.py”)
{:退出0,:退出“Hello World\n”,:err”“})
;查找单个命令“python3 hello.py”后失败
(抛出?(shell/sh“python3 hello.py”))
将每个
execve()
元素(也就是说,在实际启动新进程时传递给操作系统的字符串数组的每个元素)作为单独的参数传递:

(使用“[clojure.java.shell:仅限[sh]”)
(sh“python3”“hello.py”)

与其名称可能暗示的相反,
clojure.java.shell/sh
实际上并不调用
sh
。相反,它直接调用
Runtime.exec()
,该函数采用直接参数列表

如果要调用shell,可以显式执行以下操作:

(使用“[clojure.java.shell:仅限[sh]”)
(sh“sh”“-c”“python3 hello.py”)

或者,更好的方法是将您的
hello.py
设置为具有有效shebang的可执行文件,并直接调用它。也就是说,确保
hello.py
开头/usr/bin/env python3
,运行
chmod+x./hello.py
,然后运行use:

(使用“[clojure.java.shell:仅限[sh]”)
(sh.“/hello.py”)

(也可以考虑放弃<代码> Py <代码>扩展;正如您运行的代码> PIP而不是<代码> PIP.P< <代码>和 Ls<代码>而不是<代码> ls > ELP<代码>,可执行脚本不应该有扩展名。

如果Python 3//C> >在当前目录中,则仅需要此……
python3
本身不使用
路径
查找它被告知要执行的脚本。OP的“未找到命令”是因为没有名为
python3 hello.py
的文件在其路径中的名称中带有空格。
clojure.java.shell/sh
实际上并不启动shell、函数和命名空间名称。改为运行
(sh“python3”“hello.py”)
。按照您自己的建议,我认为最好将其标记为类似于或的副本,而不是为这种对
clojure.java.shell
工作原理的频繁误解编写另一个答案。谢谢你指出这些。
{:exit 0    ; unix exit status (0 -> normal) 
 :err ''    ; text from any errors 
 :out '...' ; text output as would printed to console 
}