1

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?

Parth Mall
  • 33
  • 1
  • 5
  • You may need to explicitly add the module to your path. `import sys` and then `sys.path.insert(0, "/path/to/your/package_or_module")` – Varun Nayak Jun 01 '20 at 07:18
  • @VarunNayak Doing that brought up the same error for other modules. After adding those modules explicitly. it eventually gave me syntax for a file in /opt/ros/indigo/lib/python2.7/dist-packages. I don't understand how can it give a syntax error inside the notebook, while it works perfectly fine otherwise. – Parth Mall Jun 01 '20 at 13:16
  • Do you have to use python3? Ros indigo was designed on-top of python2. If you want guaranteed native ros targeting python3, ros noetic for ubuntu 20 is available, if you don't have to use ubuntu 14. – JWCS Jun 01 '20 at 15:55

1 Answers1

0

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.

JWCS
  • 1,120
  • 1
  • 7
  • 17