Running bash/python script with ProcessBuilder - python

I want to run a bash script by using ProcessBuilder. This is my xtend code:
new ProcessBuilder().inheritIO().command("/bin/bash", "-c", "./myscript.sh")
This is my bash script:
#!/bin/bash
python WebRoot/result.py
And the python code:
#! /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. In your bash script, try pointing directly to the python version you intend to use. It's entirely possible that the JVM's env is pointing to a different version of 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

Related

Python script works locally but not through SSH

I’m trying to link a Raspberry pi with pycroft with a Turtlebot via SSH. I have created a skill and I want that when I said “go to somewhere” the turtlebot goes to this place.
No problem with this part. The problem come here.
I have a python script and it works fine if firstly I connect via ssh and then I execute python go_to_specific_point_on_map.py
$ ssh tb2#192.168.0.158
$ python go_to_specific_point_on_map.py
Image of everything working fine
But if I tried to do all in one command, I get:
ImportError: No module named ‘rospy’
$ ssh tb2#192.168.0.158 python go_to_specific_point_on_map.py
Image of the error. ImportError: No module named rospy
^[Traceback (most recent call last):
File "./mubita/go_to_specific_point_on_map.py", line 22, in <module>
import rospy
ImportError: No module named rospy
I have tried the arunp9294's solution but I get the same error.
$ ssh tb2#192.168.0.158 "source ~/.bashrc; python go_to_specific_point_on_map.py"
The file go_to_specific_point_on_map.py is here:
go to specific point on map script
I think it is a problem due to .bashrc is not loaded and the alias neither. I don’t know exactily what the problem is and how to solve it.
Can somebody help me? please.
I don’t know how to get it to work.
Thank you very much and best regards
I'm guessing it's a path problem, try adding the location of the rospy module to the pythonpath with:
import sys
sys.path.append("/path/to/rospy/in/your/turtlebot")
import rospy
the problem with this is that you have to manually change the path if the code is executed on different systems with different locations of the 'rospy' module
If you don't know the path you can first execute a python program like
import sys
print(sys.path)
in both ways (directly in the ssh, and after the ssh) and see if there is a difference in the output, if you see a difference just extend the path in your code with that difference.
example:
output with execution after connection:
$ ssh tb2#192.168.0.158
$ python print_path.py
['a', 'b', 'c', 'd']
output with connection and execution in one command:
$ ssh tb2#192.168.0.158 python print_path.py
['a', 'b']
if you see a difference then modify the code in the turtlebot with:
import sys
sys.path.extend(['c', 'd'])
import rospy
...

Cron python script import error

I'm trying to execute a python script on boot. The script runs fine in a shell, but when I boot I got an import error for zmq package, here's the cronlog:
Traceback (most recent call last):
File "/home/elachere/Documents/mg_app/r_server.py", line 7, in <module>
import zmq
ImportError: No module named zmq
I saw many posts about this, so far I tried :
-exports PYTHONPATH in my bash_profile and run the python script via a bash script
-add these lines to my python script (before zmq import):
#!/usr/bin/python
import sys
sys.path.append('/home/elachere/.local/lib/python2.7/site-packages/zmq/')
-set PYTHONPATH at the top of my crontab
Unfortunately nothing worked, I still get the import error.
Thanks in advance for your response.

CentOS Python multiple interpreters issue - incorrect shebang

I'm trying to run a Python script (testing.py) as a cron job. I have CentOS with a separate python2.7 interpreter path, so I've added the following shebang:
#!/usr/local/bin/python2.7
However, when the cron job runs I get the following error
Traceback (most recent call last):
File "/root/carsales/carsales_v2.py", line 3, in <module>
import pandas as pd
ImportError: No module named pandas
I have installed Anaconda, and if I run the python2.7 interpreter through the command line, as:
python2.7 testing.py
It imports pandas successfully. However, running it directly (as per below) fails to import:
./testing.py
So there's some problem with the shebang, or possibly with the Anaconda install?

ImportError where path to module is in PYTHONPATH

I'm not sure why I am still getting an ImportError
Traceback:
Traceback (most recent call last):
File "./test_jabba.py", line 12, in <module>
from tests import testbench
ImportError: No module named tests
Where error occurs:
from tests import testbench
from utils import gopher, jsonstream
I have this in my .bashrc
export PYTHONPATH=$PYTHONPATH:/Users/bli1/Development/QE/TrinityTestFramework/poc
However, when I echo $PYTHONPATH nothing is returned
I added __init__.py within directories tests and utils as well but same error occurs
Are you sure your .bashrc is correctly sourced?
If you run in your session
echo $PYTHONPATH
is it correctly set?
If not, try to manually export the path and then try to understand why your .bashrc is not being sourced. Possible trivial causes:
You are using a shell different than bash, very trivial but might happen. Try to run echo $SHELL. Note that this might return /bin/bash even if you are not actually using bash. Please refer to this post to find out which shell you are using. You can also start a new bash session manually (just run /bin/bash) and double check if PYTHONPATH is exported correctly there.
You modified your .bashrc but you didn't start a new session.
You should import the module and not the folder. The from something import something command only works for objects in the module file. Python module import error or python module import error should help

How can I fix this Python script?

I'm trying to convert a CVS repository to Git using cvs2svn and am following the directions on this page. I got to step 7 but am getting an error running git-move-refs.py:
Traceback (most recent call last):
File "../../cvs2svn-trunk/contrib/git-move-refs.py", line 23, in ?
from subprocess import Popen, PIPE, call
ImportError: No module named subprocess
For reference, this is what the script shows:
usage = 'USAGE: %prog [options]'
import sys
import optparse
from subprocess import Popen, PIPE, call
I'm not a Python expert but from browsing the web it looks like subprocess is a standard module, right? I'm using a Python installation built from source for version 2.6.3. What am I missing for this script to work?
I'm guessing that you have an old version (pre-2.4) of Python at /usr/bin/python, from your distribution, and the Python 2.6 you compiled is somewhere else (like /usr/local/bin/python). You have the Python 2.6 executable on your path before /usr/bin, so when you execute python from the command-line you get Python 2.6.
However, looking at the cvs2svn source code, git-move-refs.py's interpreter line is hard-coded to
#!/usr/bin/python
instead of #!/usr/bin/env python, which means when you run the script it uses the old Python.
As a workaround, run the script by passing it to your Python 2.6 interpreter:
user#host$ python /path/to/cvs2svn/contrib/git-move-refs.py

Categories

Resources