'Importing Rosbag in Python 3
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)
Solution 1:[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:[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:[3]
To use the standard rosbag library, you have two options:
- 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
- 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.
Solution 4:[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:
- my own comment and this answer
- [cross-linked on the ROS site] https://answers.ros.org/question/12307/no-module-named-rosbag-error/?answer=387606#post-id-387606
Solution 5:[5]
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
Solution 6:[6]
Try this and it will work:
pip3 install pyrosbag
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Astra Uvarova - Saturn's star |
| Solution 2 | simbamford |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | Steven |
| Solution 6 | Shahryar |
