为什么在我的c++程序中无法识别/usr/local/bin

Why /usr/local/bin is not recognisable in my C++ program?

本文关键字:识别 usr local bin 我的 c++ 程序 为什么      更新时间:2023-10-16

我的c++代码:

int main(int argc, const char * argv[])
{
    system("python test.py");
    return 0;
}

我在/usr/local/bin中有一个程序foo,我可以在Unix控制台中输入foo,它就启动了。

下面的Python脚本(test.py)可以工作:
from subprocess import call
call(["/usr/local/bin/foo"])

这个没有(带有"command not found"错误):

from subprocess import call
call(["foo"])

为什么我的Python脚本,当从c++执行时,不能直接调用程序?

编辑:

我用的是Mac OSX。我怀疑这与/usr/local/bin文件夹没有被c++可执行文件添加或类似的东西有关。

编辑:

 which kallisto
 /usr/local/bin/kallisto
cat test.py
from subprocess import call
call(["kallisto"])
python test.py
kallisto 0.43.0
In my C++ run:
Traceback (most recent call last):
  File "/Users/tedwong/Sources/QA/test.py", line 2, in <module>
    call(["kallisto"])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory 

感谢@ilent2,他的回答解决了我的问题。我使用IDE来运行c++代码,从来没有意识到我必须告诉IDE我的路径。当我直接在控制台中运行相同的程序时,它工作了。