Importing Rosbag in Python 3

11,368

Solution 1

You can use the bagpy package to read the .bag file in Python. It can be installed using pip

pip install bagpy

Brief documentation is at https://jmscslgroup.github.io/bagpy/

Following are example code-snippets:

import bagpy
from bagpy import bagreader

b = bagreader('09-23-59.bag')

# get the list of topics
print(b.topic_table)

# get all the messages of type velocity
velmsgs   = b.vel_data()
veldf = pd.read_csv(velmsgs[0])
plt.plot(veldf['Time'], veldf['linear.x'])

# quickly plot velocities
b.plot_vel(save_fig=True)

# you can animate a timeseries data
bagpy.animate_timeseries(veldf['Time'], veldf['linear.x'], title='Velocity Timeseries Plot')

Solution 2

I've written a pure python3 module for importing rosbag data. It's standalone - no ROS installation required. It only works for a selected subset of the message types but it should serve as an example which you can follow to unpack the message types that you're interested in: https://github.com/event-driven-robotics/importRosbag

Solution 3

You can use the ROS-specific PyPI index for rosbag and other ROS Python packages:

pip install rosbag --extra-index-url https://rospypi.github.io/simple/

If you have already sourced /opt/ros/$ROS_DISTRO/setup.bash, which adds its Python packages to PYTHONPATH, you might also need to do unset PYTHONPATH first.

You can also install roslz4 for LZ4 compression support.

Solution 4

TLDR;

Run this:

pip3 install bagpy
# OR, if that ends up failing in the end due to a "Permission denied" error, 
# do this:
sudo pip3 install bagpy

Now this will work in your Python 3 script:

import rosbag

...so long as you have the correct hash-bang at the top of your Python 3 file, such as this one:

#!/usr/bin/env python3

Details

I have written ros_readbagfile and this ROS tutorial here: Reading messages from a bag file, and this ModuleNotFoundError: No module named 'rosbag' error seems to come up a lot:

Traceback (most recent call last):
  File "./ros_readbagfile", line 50, in <module>
    import rosbag
ModuleNotFoundError: No module named 'rosbag'

The solution to get import rosbag to work in Python 3 seems to be:

pip3 install bagpy

Now import rosbag works, and therefore, so does my ros_readbagfile script.

According to this answer, you can apparently also do:

conda install -c conda-forge ros-rosbag 

...but I haven't tried that.

If you have run pip3 install bagpy and it fails to complete due to permissions errors:

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied

...then try using sudo as well:

sudo pip3 install bagpy

Now, assuming that worked, if import rosbag still doesn't work, then it may be because pip3 install bagpy installed bagpy (and rosbag) for a different binary executable of Python3 than what your script is calling via its hash-bang line at the top. To see if that's the case, run which python3 and use that path at the top of your Python3 script. Ex: my which python3 output is:

/usr/bin/python3

So, my hashbang (shebang) at the top of my Python3 script should be:

#!/usr/bin/python3

Other common paths may include:

/usr/local/bin/python3

Or (BEST in this case), to let your environment choose the python3 executable for you, use this hash-bang at the top of your Python 3 file instead:

#!/usr/bin/env python3

This is what I now use at the top of my ros_readbagfile.py script.

Other references:

  1. my own comment and this answer
  2. [cross-linked on the ROS site] https://answers.ros.org/question/12307/no-module-named-rosbag-error/?answer=387606#post-id-387606
Share:
11,368
Lovro
Author by

Lovro

Updated on June 09, 2022

Comments

  • Lovro
    Lovro almost 2 years

    I'm trying to read rosbag files from Python 3.
    I installed ROS2 (Eloquent Elusor), which should support Python 3.

    When I run

    import rosbag
    bag = rosbag.Bag('test.bag')
    

    from Python 2.7, it works.
    When I try the same in Python 3, I get:

    ModuleNotFoundError: No module named 'rosbag'
    

    I also tried things like: sudo apt install python-rosbag, sudo apt install python3-rospkg and pip3 install rospkg, but they don't help.

    What should I do to open a rosbag file from Python 3?

    [EDIT]
    This is the output after calling pip3 install rospkg:

    Requirement already satisfied: rospkg in ./rosbag-env/lib/python3.6/site-packages
    Requirement already satisfied: catkin-pkg in ./rosbag-env/lib/python3.6/site-packages (from rospkg)
    Requirement already satisfied: distro in ./rosbag-env/lib/python3.6/site-packages (from rospkg)
    Requirement already satisfied: PyYAML in ./rosbag-env/lib/python3.6/site-packages (from rospkg)
    Requirement already satisfied: pyparsing in ./rosbag-env/lib/python3.6/site-packages (from catkin-pkg->rospkg)
    Requirement already satisfied: python-dateutil in ./rosbag-env/lib/python3.6/site-packages (from catkin-pkg->rospkg)
    Requirement already satisfied: docutils in ./rosbag-env/lib/python3.6/site-packages (from catkin-pkg->rospkg)
    Requirement already satisfied: six>=1.5 in ./rosbag-env/lib/python3.6/site-packages (from python-dateutil->catkin-pkg->rospkg)