Python script works locally but not through SSH - python

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
...

Related

ModuleNotFoundError when importing from a ROS package

I have use a robot simulator which only works on ROS Indigo. I also have to use Tensorflow and Jupyter Notebook so I have created a virtual environment with Python3.6.
I want to import a file from the simulator package in a notebook. So after I source bin/activate for the virtualenv, I source devel/setup.bash from my catkin workspace. But even after doing this I get the following error
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-8-8370e76446ee> in <module>
----> 1 import herbpy
~/my_workspace/devel/lib/python2.7/dist-packages/herbpy/__init__.py in <module>
33 for __execfile in __execfiles:
34 with open(__execfile, 'r') as __fh:
---> 35 exec(__fh.read())
36 del __fh
37 del __execfile
~/my_workspace/devel/lib/python2.7/dist-packages/herbpy/__init__.py in <module>
ModuleNotFoundError: No module named 'herb'
I tried running a python file importing this module (while in the environment). It only ran with python2. But with python2, I can't use Tensorflow.
How do I import this file in my notebook?
There's a few things. Remember that you can specify the version of python to run via the shebang in your script:
#!/usr/bin/env python # For "both" versions, not recommended for ros
#!/usr/bin/env python2 # For ros, to access the python/pip packages only installed for py2
#!/usr/bin/env python3 # For python3 code, to avoid that 'python' usually selects py2
You can call different python executables from launch files this way:
<?xml version="1.0"?>
<launch>
<!-- py3_ex.launch -->
<node name="call_py3" pkg="my_new_pkg" type="call_py3.py" output="screen">
</node>
</launch>
#!/usr/bin/env python3
# call_py3.py
# Try to print out the python version
import sys
import platform
#import rospy
#rospy.init_node("call_py3");
print("\n\nPython Version:\n")
print(sys.version)
print(platform.python_version())
print("\n\n")
#rospy.spin()
The above script will run python3 code (printing the version number), including tensorflow. The issue is that it, on trying to import rospy on py3 instead of py2, it won't find some of it's dependencies (such as rospkg) and give a fatal error.
To get around this, (assuming you don't switch to Ubuntu 20 + ROS Noetic, with native py3 support, or just use py2), you can call py3 scripts from your py2 ros node, such as with subprocess and piping. Or better yet, socket udp send over localhost your data/images/results/inputs, that might be the simplest way. To go this way, (and it works!) make a import socket udp connection to a localhost (127.0.0.1) random port (ex 5050), on both sides. Launch both scripts. Your py3 script can't import rospy but it can import ros msgs: from sensor_msgs.msg import Image. You can find a means to serialize it (there's multiple ways to turn a py obj into string and back).
A more painful way to setup python3 (but should be easier to use after) is to somehow replace the python2 packages to use ros with python3. There are a bunch of guides out there, and plenty of people who've ran into issues posted on SO.

I can't import a module in Python/Windows

I'm having problem when I trying to import a custom module I have which is very simple but I'm always getting:
Traceback (most recent call last):
File "demo_module1.py", line 12, in <module>
import mymodule
ModuleNotFoundError: No module named 'mymodule'
I have tried to set environment variables:
set PYTHONHOME=C:\Software\python-3.7.4
set PYTHONPATH=%PYTHONPATH%;C:\pyproys\test
Everything is located here: 'C:\pyproys\test'
The only way it works is if I add it directly in the code "But I don't want to do it in every single script I have so don't want to maintain it in that way".
import sys
sys.path.append('C:\pyproys\\test')
print(sys.path)
Here is the script I'm trying to run:
demo_module1.py
import mymodule
mymodule.greeting("Jonathan")
'mymodule.py' is in the same folder as 'demo_module1.py'
I'm expecting the code to run fine by just executing:
python demo_module1.py
Can someone please point me out what I'm doing wrong?
Try to find the directory /lib/site-packages in your Python folder in C drive and paste your module in that folder and then restart the system. Hope it'll solve your issue.

Running bash/python script with ProcessBuilder

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

Python: ImportError: No module named connector

When I execute a Python script I receive:
Traceback (most recent call last):
File "/Users/.../Documents/development/python/migrate_upper.py", line 3, in <module>
import mysql.connector
ImportError: No module named connector
I am executing like that:
$ python migrate_upper.py
It worked 1 month ago, I haven't worked with Python since then. I spent 2 hours trying to understand what is wrong but I got lost with PYTHONPATH, pip and other hints.
However when I send the script to Python shell:
$ python < migrate_upper.py
everything works. I think that this is not the proper way to execute python scripts. How can I get the script working without the Python shell?
I resolved it with the help of Ethan's comment.
I had a folder mysql beneath ../development/python. I can't remember why I put it there. I guess python tried to import that folder instead of ../site-packages/mysql.

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

Categories

Resources