简体   繁体   English

使用 ProcessBuilder 运行 bash/python 脚本

[英]Running bash/python script with ProcessBuilder

I want to run a bash script by using ProcessBuilder.我想使用 ProcessBuilder 运行 bash 脚本。 This is my xtend code:这是我的 xtend 代码:

new ProcessBuilder().inheritIO().command("/bin/bash", "-c", "./myscript.sh")

This is my bash script:这是我的 bash 脚本:

#!/bin/bash
python WebRoot/result.py

And the python code:和python代码:

#! /usr/bin/env python
import rospy
from std_msgs.msg import Empty
...

The problem is that I get an error:问题是我收到一个错误:

Traceback (most recent call last):
  File "WebRoot/result.py", line 2, in <module>
    import rospy
ImportError: No module named rospy

However, when I run the code manually via terminal, it works fine.但是,当我通过终端手动运行代码时,它工作正常。

When you run it command line, you are probably getting a different environment from when it's running in your JVM.当您在命令行中运行它时,您可能会获得与它在 JVM 中运行时不同的环境。 In your bash script, try pointing directly to the python version you intend to use.在您的 bash 脚本中,尝试直接指向您打算使用的 python 版本。 It's entirely possible that the JVM's env is pointing to a different version of python. JVM 的 env 完全有可能指向不同版本的 python。 Or that the environment is not fully setup.或者环境没有完全设置好。

Try to put the full path, like this, for example:尝试放置完整路径,例如:

#!/bin/bash
/usr/bin/python2.7/python WebRoot/result.py

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

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