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)
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
Installing bagpy apparently also installs rosbag.
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
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With