bash python os.system问题:“sh:1:[命令]未找到”;命令以交互方式工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42677066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python os.system issue: "sh: 1: [command] not found"; command works interactively
提问by Kestrel
I'm trying to run a system call from Python. I have a line that reads this in my Python script:
我正在尝试从 Python 运行系统调用。我有一行在我的 Python 脚本中读取此内容:
return os.system("crux tide-index")
crux
is a program existing in my /home/
directory, and if I type the command crux tide-index
into a terminal, it seems to work properly.
crux
是我/home/
目录中的一个程序,如果我在crux tide-index
终端中键入命令,它似乎可以正常工作。
When I run my Python script, it reaches the line above and then outputs this line to stderr (i.e. it shows up in the output of my terminal):
当我运行 Python 脚本时,它到达上面的行,然后将此行输出到 stderr(即它显示在我的终端的输出中):
sh: 1: crux not found
sh: 1: crux not found
I don't understand why I can run the command in my terminal, but not in a Python script. Is there something I'm missing? Is the fact that crux
is in my /home/
folder possibly the problem?
我不明白为什么我可以在终端中运行命令,但不能在 Python 脚本中运行。有什么我想念的吗?crux
我的/home/
文件夹中的事实可能是问题吗?
回答by Charles Duffy
Possible Reasons
可能的原因
There are several reasons you could be able to run this in your terminal, but not in a Python script.
有几个原因可以让您在终端中运行它,但不能在 Python 脚本中运行。
It could be defined as an alias.
If you have
alias crux=~/crux
in your.bashrc
or similar, that would explain the issue.It could be defined as a function.
crux() { ~/crux "$@"; }
is an example of a shell function that starts thecrux
executable. However, like any other function, this is local to the shell wherein it's defined. (Bash has "exported functions", but these aren't available in POSIX sh, and you need to go out of your way to use them anyhow).You could have a different PATH between your interactive CLI and your script.
If you have
PATH=$PATH:$HOME
somewhere in your shell's dotfiles, this will add your home directory to the location searched for new executables. On its own, this willbe exported to subprocesses, so searched by the/bin/sh
instance started byos.system()
in Python -- but if your script is being started bycron
or another service, it wouldn't have that PATH update.
它可以定义为别名。
如果你有
alias crux=~/crux
你的.bashrc
或类似的,那将解释这个问题。它可以定义为一个函数。
crux() { ~/crux "$@"; }
是启动crux
可执行文件的 shell 函数的示例。但是,与任何其他函数一样,这是定义它的 shell 的本地函数。(Bash 具有“导出函数”,但这些在 POSIX sh 中不可用,无论如何您都需要特意使用它们)。您可以在交互式 CLI 和脚本之间使用不同的 PATH。
如果您
PATH=$PATH:$HOME
在 shell 的点文件中有某个位置,这会将您的主目录添加到搜索新可执行文件的位置。就其本身而言,这将被导出到子进程,因此/bin/sh
通过os.system()
在 Python 中启动的实例进行搜索——但是如果您的脚本是由cron
或其他服务启动的,则它不会有那个 PATH 更新。
Debugging
调试
Run type crux
in your interactive shell. Output will be of the form of one of the following:
type crux
在交互式 shell 中运行。输出将采用以下形式之一:
crux is aliased to `/home/kestrel/crux'
means that it works in your interactive shell only due to an alias. Update your PATH to include/home/kestrel
, or modify your Python script to fully-qualify the script's location....or if you get:
crux is a function crux () { /home/kestrel/crux "$@" }
...it means exactly what it says:
crux
is a function that in turn invokes/home/kestrel/crux
. You can then put/home/kestrel/crux
directly in your Python script....or if you get either of:
crux is hashed (/home/kestrel/crux)
or
crux is /home/kestrel/crux
then
crux
is directly in the PATH for your interactive command prompt, but not for your Python script. Edit the PATH associated with your script appropriately.
crux is aliased to `/home/kestrel/crux'
意味着它仅由于别名而在您的交互式 shell 中工作。更新您的 PATH 以包含/home/kestrel
,或修改您的 Python 脚本以完全限定脚本的位置。...或者如果你得到:
crux is a function crux () { /home/kestrel/crux "$@" }
...它的意思正是它所说的:
crux
是一个函数,它反过来调用/home/kestrel/crux
. 然后,您可以/home/kestrel/crux
直接放入 Python 脚本。...或者如果你得到以下任何一个:
crux is hashed (/home/kestrel/crux)
或者
crux is /home/kestrel/crux
then
crux
直接位于交互式命令提示符的 PATH 中,而不是 Python 脚本的 PATH 中。适当地编辑与您的脚本关联的 PATH。