GDB pretty printing ImportError: No module named 'printers'
Asked Answered
A

1

7

I'm trying to add pretty printing for STL in my GDB on Ubuntu 14.04. Some details on the tools:

OS: Ubuntu 14.04

gdb version: 7.7

python version: 2.7.6

python3 version: 3.4.0

But after I setup exactly as what the instruction said. I still get the following errors:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/home/jerry/myLib/gdb_stl_support/python/libstdcxx/v6/__init__.py", line 19, in <module>
    from printers import register_libstdcxx_printers
ImportError: No module named 'printers'
/home/jerry/.gdbinit:6: Error in sourced command file:
Error while executing Python code.
Reading symbols from main...done.

Then I haved double checked my pretty printing installation directory. Under the directory /home/jerry/myLib/gdb_stl_support/python/libstdcxx/v6/, I can clearly see I have the printers.py file. And I also view the content of printers.py, I'm sure it also has the register_libstdcxx_printers class. Why the python interpreter is still complaining the printers module is missing? This seems really strange to me.

Actor answered 5/10, 2014 at 17:58 Comment(0)
A
11

I just tried something myself, and luckily, now it's working. At least it can print out the map and vector content as expected. Here is what I did:

Since it's complaining that it can't find the printer.py module, then I think should probably I tell python interpreter where this file is located. So I first added this extra line to my ~/.gdbinit: sys.path.append("/home/jerry/myLib/gdb_stl_support/python/libstdcxx/v6")

(After the line sys.path.insert(0, '/home/jerry/myLib/gdb_stl_support/python') )

Then running gdb again, I got the following error:

Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "/home/jerry/myLib/gdb_stl_support/python/libstdcxx/v6/printers.py", line 1247, in register_libstdcxx_printers
    gdb.printing.register_pretty_printer(obj, libstdcxx_printer)
  File "/usr/share/gdb/python/gdb/printing.py", line 146, in register_pretty_printer
    printer.name)
RuntimeError: pretty-printer already registered: libstdc++-v6
/home/jerry/.gdbinit:7: Error in sourced command file:
Error while executing Python code.

Given the error information, I edited the ~/.gdbinit file and commented the line register_libstdcxx_printers (None).

And then after running gdb, it works.

But I'm still wondering if directory in sys.path is searched recursively? I mean in my mind, the python interpreter should work like this: once you have added one directory to sys.path, then the subdirectory under that directory will also get searched for a module file.

Actor answered 5/10, 2014 at 18:37 Comment(3)
No, there is no "search" for module files. If a directory in sys.path contains other directories, these must be packages for you to be able to import modules from within. But then you need to give the package-name in the import statement. E.g. "import gdb" works, and "import gdb.printing" works, but not "import printing".Frei
@Frei Thank you for mentioning the packages concepts in python. I don't know python is organizing submodules like this.Actor
Editionally I needed to edit python/libstdcxx/v6/__init__.py and replace from printers import register_libstdcxx_printers with from .printers import register_libstdcxx_printers (My GDB includes Python 3.4)Koehn

© 2022 - 2024 — McMap. All rights reserved.