python os.system问题:"sh:1:未找到[command]";命令可以交互工作 [英] python os.system issue: "sh: 1: [command] not found"; command works interactively

查看:117
本文介绍了python os.system问题:"sh:1:未找到[command]";命令可以交互工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Python运行系统调用.我有一行在我的Python脚本中读取:

I'm trying to run a system call from Python. I have a line that reads this in my Python script:

return os.system("crux tide-index")

crux 是我的/home/目录中存在的程序,如果我在终端中键入命令 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.

当我运行Python脚本时,它到达上面的行,然后将此行输出到stderr(即,它显示在终端的输出中):

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):

sh:1:找不到关键

我不明白为什么我可以在终端中运行命令,但不能在Python脚本中运行命令.有什么我想念的吗?问题 crux 是否在我的/home/文件夹中,这可能是问题所在吗?

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脚本中运行.

Possible Reasons

There are several reasons you could be able to run this in your terminal, but not in a Python script.

  • 可以将其定义为别名.

  • It could be defined as an alias.

如果您的 .bashrc 或类似文件中有 alias crux =〜/crux ,这将解释该问题.

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"$ @";} 是启动 crux 可执行文件的Shell函数的示例.但是,就像其他任何函数一样,这对于定义它的外壳来说是局部的.(Bash具有导出的功能",但是在POSIX sh中不可用,因此您必须竭尽所能来使用它们).

crux() { ~/crux "$@"; } is an example of a shell function that starts the crux 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).

您在交互式CLI和脚本之间可以具有不同的PATH.

You could have a different PATH between your interactive CLI and your script.

如果您的Shell的点文件中有 PATH = $ PATH:$ HOME ,这会将您的主目录添加到搜索新可执行文件的位置.此将会单独导出到子进程,因此可以通过 os.system()在Python中启动的/bin/sh 实例进行搜索-但是,如果您的脚本是由 cron 或其他服务启动的,则不会进行PATH更新.

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 will be exported to subprocesses, so searched by the /bin/sh instance started by os.system() in Python -- but if your script is being started by cron or another service, it wouldn't have that PATH update.

在交互式shell中运行 type crux .输出将采用以下其中一种形式:

Run type crux in your interactive shell. Output will be of the form of one of the following:

  • crux的别名为`/home/kestrel/crux',这意味着它仅在别名的情况下才能在您的交互式Shell中工作.更新您的PATH以包含/home/kestrel ,或修改Python脚本以完全限定脚本的位置.
  • ...或者如果得到:

  • 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 "$@"
}

...它的含义恰恰是它的意思: crux 是依次调用/home/kestrel/crux 的函数.然后,您可以直接在Python脚本中放置/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)

crux is /home/kestrel/crux

然后, crux 直接位于PATH中,用于您的交互式命令提示符,但不适用于您的Python脚本.适当编辑与脚本关联的PATH.

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.

这篇关于python os.system问题:"sh:1:未找到[command]";命令可以交互工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆