简体   繁体   English

如何在Mac OS上使用python subprocess.check_output linux命令

[英]How to python subprocess.check_output linux command on mac os

I am trying to run a linux executable on Max OS X 10.11.6 via python2.7 我正在尝试通过python2.7在Max OS X 10.11.6上运行linux可执行文件

I would like to use subprocess.check_output. 我想使用subprocess.check_output。

The command, which works via the terminal is: 通过终端运行的命令是:

mosel -c "exec PATH/TO/SCRIPT arg1='value1', arg2='value2'"

However, when I try: 但是,当我尝试:

subprocess.check_output(['mosel','-c',cmd])

where 哪里

cmd="exec PATH/TO/SCRIPT arg1='value1', arg2='value2'"'

I get: 我得到:

File "/usr/local/lib/python2.7/site-packages/subprocess32.py", line 629, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/local/lib/python2.7/site-packages/subprocess32.py", line 825, in __init__
restore_signals, start_new_session)
File "/usr/local/lib/python2.7/site-packages/subprocess32.py", line 1574, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 2] No such file or directory: 'mosel'

I have been able to get it to "echo" the command to an output file, but I cannot run "which mosel" via python, which leads me to believe that it has to do with check_output using "bin/sh"as the executable. 我已经能够将其“回显”命令到输出文件,但是我无法通过python运行“哪个Mosel”,这使我相信它与使用“ bin / sh”作为可执行文件的check_output有关。

So, do I need to use "Popen" instead and set 所以,我需要使用“ Popen”代替并设置

executable=path/to/mosel

?

If so, how do use Python to get the user's path to mosel (ie get the output of "which mosel")? 如果是这样,如何使用Python获取用户的Mosel路径(即获取“哪个Mosel”的输出)?

Thanks! 谢谢!

UPDATE: 更新:

PyCharm was not seeing the system paths, which I fixed using this answer: Setting environment variables in OS X? PyCharm没有看到系统路径,我使用以下答案对此进行了修复: 在OS X中设置环境变量?

Now, it appears that 现在看来

subprocess.check_output(['mosel','-c',cmd])

Is sending the square brackets to the command line, because it now returns: 正在将方括号发送到命令行,因为它现在返回:

dyld: Library not loaded: libxprm_mc.dylib
Referenced from: /usr/local/opt/xpress/bin/mosel
Reason: image not found
Traceback (most recent call last):
File "/Users/nlaws/projects/sunlamp/sunlamp-ss/RunScenarios/run.py", line 70, in <module>
run(1)
File "/Users/nlaws/projects/sunlamp/sunlamp-ss/RunScenarios/run.py", line 44, in run
out = check_output(['mosel', '-c', cmd])
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 219, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['mosel', '-c', cmd]' returned non-zero exit status -5

Or is there still a path issue?! 还是还有路径问题? (I can run mosel -c cmd via the mac terminal, but not in pycharm via python, nor in the mac terminal via python). (我可以通过mac终端运行mosel -c cmd ,但是不能通过python在pycharm中运行,也不能通过python在mac终端中运行)。

The problem is you're using check_output 's arguments incorrectly. 问题是您使用的check_output参数不正确。 Unless you pass it shell=True , check_output expects a list of parameters as its input , in the form: check_call(['binary_name','arg1','arg2','arg3', ....]) 除非您将其传递给shell=Truecheck_output 期望使用以下形式的参数列表作为输入 ,形式为: check_call(['binary_name','arg1','arg2','arg3', ....])

So in this case, you should do: 因此,在这种情况下,您应该执行以下操作:

subprocess.check_call(['mosel', '-c', "exec PATH/TO/SCRIPT arg1='value1', arg2='value2'"])

The root of the issue turns out to be the DYLD_LIBRARY_PATH : 问题的根源是DYLD_LIBRARY_PATH

The new OS X release 10.11 "El Capitan" has a "security" feature that prevents passing DYLD_LIBRARY_PATH to child processes. 新的OS X版本10.11“ El Capitan”具有“安全性”功能,可防止将DYLD_LIBRARY_PATH传递给子进程。 Somehow, that variable is stripped from the environment. 不知何故,该变量已从环境中剥离。 - (quoted from https://www.postgresql.org/message-id/20151103113612.GW11897@awork2.anarazel.de ) -(引自https://www.postgresql.org/message-id/20151103113612.GW11897@awork2.anarazel.de

The security feature is called SIP or "System Integrity Protection". 该安全功能称为SIP或“系统完整性保护”。 Unfortunately, it seems that no one has come up with a solution to this issue (other than work-arounds that must be tailored to each situation). 不幸的是,似乎没有人想出解决这个问题的方法(必须针对每种情况量身定制的解决方法除外)。

Here is another example of this issue: https://github.com/soumith/cudnn.torch/issues/111 这是此问题的另一个示例: https : //github.com/soumith/cudnn.torch/issues/111

Google "mac os inherit dyld_library_path" and you will find many other examples. Google“ mac os继承了dyld_library_path”,您将找到许多其他示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM