5

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)
Lovro
  • 712
  • 1
  • 10
  • 20
  • It is my understanding that the rosbag library is only python2.7 and from what i have been able to research does not exist on Python3 yet. pyrosbag is not a stable library and i would not suggest it.There are a couple tutorials to use python2.7 libs in python3 though this feels kind of hacky to me. im in the same boat as you at the moment. – castaway2000 Jan 17 '20 at 23:04
  • See also: [ModuleNotFoundError: No module named 'rosbag' with pip](https://stackoverflow.com/questions/57254026/modulenotfounderror-no-module-named-rosbag-with-pip) – Gabriel Staples Sep 28 '21 at 21:35
  • https://jmscslgroup.github.io/bagpy/index.html – Rexcirus Jul 19 '22 at 13:37

6 Answers6

5

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')

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

simbamford
  • 71
  • 1
  • 9
2

To use the standard rosbag library, you have two options:

  1. Set up ROS and install it via apt:
sudo apt install ros-$ROS_DISTRO-rosbag ros-$ROS_DISTRO-roslz4
source /opt/ros/$ROS_DISTRO/setup.bash
  1. Install it as a regular Python package from pip, albeit not from the regular PyPI index:
pip install rosbag roslz4 --extra-index-url https://rospypi.github.io/simple/

Note that if you are have already sourced /opt/ros/$ROS_DISTRO/setup.bash, which adds the ROS Python packages to PYTHONPATH, you might need to do unset PYTHONPATH first for the pip-installed package to be used. This is especially important if you are using Python from within a virtualenv or a Conda environment.

Martin Valgur
  • 5,793
  • 1
  • 33
  • 45
2

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
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
0

According to http://wiki.ros.org/rosbag/Cookbook it says that you have to do the following in pip3 to make rosbag work:

pip3 install pycryptodomex python-gnupg

Worked for me so far. Hope that might help all others as well.

Setup:

  • Ubuntu 18.04
  • ROS Melodic
  • Python3
Steven
  • 170
  • 3
  • 10
-3

Try this and it will work:

pip3 install pyrosbag
Shahryar
  • 324
  • 1
  • 3
  • 17