ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

AttributeError: module 'rosbag' has no attribute 'Bag'

asked 2018-05-10 03:34:11 -0600

jmm2270 gravatar image

updated 2018-05-12 17:07:18 -0600

jayess gravatar image

I apologize if this is a seemingly very simple question, but I am stumped. I am trying to do the tutorials for rosbag: http://wiki.ros.org/rosbag/Code%20API using the Python example for read, and I get the following error:

Traceback (most recent call last):
  File "rosbag.py", line 1, in <module>
    import rosbag
  File "/home/jason/Documents/Testing_rospy/rosbag.py", line 3, in <module>
    bag = rosbag.Bag('test.bag')
AttributeError: module 'rosbag' has no attribute 'Bag'

I am using a VM with Ubuntu 16.04 32-bit running ROS kinetic. I assume there's a dependency I'm missing for using rosbag with python (3.6.1, editing with Sublime Text). I am able to run the yaml import from 1.3 here: http://wiki.ros.org/rosbag/Cookbook and I can get the info from the terminal and view the bag msgs in rqt. My python IDE works fine since I can create .py files and run them from the terminal with expected behavior.

I tried to look through as many files as I could and I've seen things on configuring the PYTHONPATH correctly, but nothing specific to rosbag. I would greatly appreciate it if someone could point me in the right direction.

Thanks!

edit retag flag offensive close merge delete

Comments

If you installed ROS using apt (or apt-get) then there is no Python 3 support. But I would have expected the import to already fail.

If you installed things in a different way, then please tell us how, as that is going to be important.

gvdhoorn gravatar image gvdhoorn  ( 2018-05-10 03:48:59 -0600 )edit

Thanks for the quick response. I installed python using apt-get. Is there a tutorial I can reference to do things the correct way? For example, am I supposed to execute my python code using "python file.py" or some other way with ros? Again, sorry for the how basic this question is.

jmm2270 gravatar image jmm2270  ( 2018-05-10 04:17:09 -0600 )edit
2

My comment was not about how you installed Python, but ROS. The default ROS pkgs don't support Python 3. You're using 3.6.1 according to your description. The ROS pkgs you install using apt-get don't support Python 3.

gvdhoorn gravatar image gvdhoorn  ( 2018-05-10 05:48:16 -0600 )edit
2

In addition: you have named your own script rosbag.py. That is not a good idea, as it will shadow (ie: hide) the rosbag that you're trying to use.

gvdhoorn gravatar image gvdhoorn  ( 2018-05-10 05:49:10 -0600 )edit

Thanks for the tips. Here's what I did today: set up a new environment with python 2.7.15 and reinstalled all dependencies (I think), then renamed my python script. I tried just simply to "import rosbag" and now get the error "ImportError: No module named rospkg". Thanks for your help.

jmm2270 gravatar image jmm2270  ( 2018-05-11 23:33:54 -0600 )edit
1

Could you please describe how you installed ROS? You still haven't answered that question.

Also: what do you mean exactly with "set up a new environment with python 2.7.15 and reinstalled all dependencies"? Are you using virtualenv with pip?

gvdhoorn gravatar image gvdhoorn  ( 2018-05-12 02:52:16 -0600 )edit

So I installed ROS using sudo apt-install directly following directions from the ROS tutorial. I was trying a virtual env with anaconda. I just decided it may be easier to setup a new VM with virtual box using Ubuntu 16.04. My end goal is to simply process rosbags from an AV using python.

jmm2270 gravatar image jmm2270  ( 2018-05-12 12:50:10 -0600 )edit

My question is now: if I start from scratch with a fresh install of Ubuntu, do I need to install python before ROS? Does the ROS full install include all the libraries I need to read from rosbags in python? I will be editing with sublime text. Thanks!

jmm2270 gravatar image jmm2270  ( 2018-05-12 12:52:29 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-05-12 13:31:44 -0600

gvdhoorn gravatar image

updated 2018-05-12 13:35:43 -0600

So I installed ROS using sudo apt-install directly following directions from the ROS tutorial.

Ok. That should install Python 2.7 (on Ubuntu 16.04) as well (if it wasn't already installed). All (well, most) ROS Python packages are only released for Python 2.

I was trying a virtual env with anaconda.

Ah. That will potentially complicate things. There are plenty of Q&A's on ROS Answers about Anaconda and the ROS Python packages.

If you're just starting out, you may want to focus on getting the basics working first, before trying to use 'alternative' Python interpreters.

I just decided it may be easier to setup a new VM with virtual box using Ubuntu 16.04. My end goal is to simply process rosbags from an AV using python.

I'm not sure what an 'AV' is, but I don't think you'll need to create a new virtual machine. Just remove Anaconda from your PATH and use the system default python (v2). That should work immediately.

My question is now: if I start from scratch with a fresh install of Ubuntu, do I need to install python before ROS?

No. The Ubuntu package manager (apt) will resolve those dependencies on its own. It'll install the regular Python 2 for you, if that isn't already present on your system.

Does the ROS full install include all the libraries I need to read from rosbags in python?

Typically, yes. Especially if you do a "desktop full" install.

I will be editing with sublime text.

The editor you'll be using does not influence any of this.

set up a new environment with python 2.7.15 and reinstalled all dependencies (I think),

If with 'setup a new environment' you mean a new 'virtualenv' and with "(re)installed all dependencies" you mean you used pip to install a nr of ROS Python pkgs then that can work, but you have to keep in mind that anything you install using pip will not be automatically considered for updates whenever those are released (ie: your package manager (apt) will not know those are installed). In general using pip to install ROS Python packages is not something that is recommended on platforms where binaries are available (it does work though).

edit flag offensive delete link more

Comments

Ah, ok, this all makes sense. I created a brand new virtualmachine running Ubuntu and I will just go through the standard install tutorial. I managed to mess up some other things while trying to fix my ptoblems. I will report back later today and mark this as the correct answer if I'm good. Thanks!

jmm2270 gravatar image jmm2270  ( 2018-05-12 18:14:26 -0600 )edit

All is working now, thanks! AV stands for autonomous vehicle :)

jmm2270 gravatar image jmm2270  ( 2018-05-13 21:17:40 -0600 )edit

Question Tools

Stats

Asked: 2018-05-10 03:34:11 -0600

Seen: 3,260 times

Last updated: May 12 '18