Take the 2-minute tour ×

I am using python 2.7 & python 3.1.3. But in my python i am unable to "import gdb"

It is giving me error as

>>> import gdb
Traceback (most recent call last):
ImportError: No module named gdb

Whats a reason for this, how should i has to solve this problem.

share|improve this question
Maybe a dup of stackoverflow.com/questions/3482869/… –  Donovan Jan 25 '11 at 10:45
Alberteddu have u worked on this. cos i am unable to understand the documentation given in the link. Can you please guide me. –  Sagar Gupta M. Jan 25 '11 at 11:03
What's your OS? –  Donovan Jan 25 '11 at 11:18
my os is windows xp –  Sagar Gupta M. Jan 25 '11 at 14:19
You did not give enough information in your question. Where did you hear about this file, and what are you expecting it to do? What are you trying to achieve? –  Sven Marnach Jan 27 '11 at 15:11
show 3 more comments

4 Answers

import gdb only works when your Python code in running within the GDB process. It's not supposed to work from the regular system Python interpreter.

  • GDB embeds the Python interpreter so it can use Python as an extension language.
  • You can't just import gdb from /usr/bin/python like it's an ordinary Python library because GDB isn't structured as a library.
  • What you can do is source MY-SCRIPT.py from within gdb which is equivalent to starting gdb with gdb -x MY-SCRIPT.py.

Here's an example, save the file below to t.py:

import gdb
gdb.execute('file /bin/cat')
print o


$ gdb -q -x t.py 

and you'll see the PLT stub for exit() disassembled. On x86-64 Linux:

Dump of assembler code for function exit@plt:
   0x0000000000401ae0 <+0>:  jmpq   *0x20971a(%rip)    # 0x60b200 <exit@got.plt>
   0x0000000000401ae6 <+6>:  pushq  $0x3d
   0x0000000000401aeb <+11>: jmpq   0x401700
End of assembler dump.

I collected some resources on learning the GDB Python API here.

share|improve this answer
add comment

You can follow this tutorial to install PythonGDB. The Python code depends on a C extension.

For Windows, there is a recent enough gdb build in MinGW, but it doesn't seem to include the Python module you can import (still supports Python scripting in gdb). You have to install MinGW and then install the gbd package using mingw-get install gdb.

If you use Cygwin, there's again a recent enough gdb in Cygwin Ports, without a Python module but with Python scripting support.

I suppose it'd be possible to build gdb from source in either platform and get the Python module.

share|improve this answer
PythonGDB does not include any file of this name (at least not in my installation). There is a file of this name in Pydb though. With the information given, it's imply undecidable what the OP is after. –  Sven Marnach Jan 27 '11 at 15:21
There's a gdb module, though, so he can indeed import gdb. –  TryPyPy Jan 27 '11 at 15:23
Are you able to run gdb by itself on Windows? How did you install it? –  TryPyPy Jan 27 '11 at 15:35
Sigh, there's no gdb module in MinGW's gdb. –  TryPyPy Jan 27 '11 at 15:46
Ok TryPyPy, i will try to give some more information as much as possible. I am working in WINDOWS XP, trying to build a script in PYTHONWIN32, i am writing this script so that it has to invoke a shell provided by MinGW, there it has to run gdb. In this gdb python script as to run my program. Every thing has to controlled by python script. 1st layer -> Python Script 2nd layes -> gdb 3rd layer -> my program. –  Sagar Gupta M. Jan 28 '11 at 6:07
add comment

I can't test now, but I think you need to configure and build a python enabled gdb. Take a look at this guide.

Hope that helps.

Edit: this is outdated, I think. Anyway, you always need to build and configure a python enabled gdb.

You can script gdb using the Python programming language. This feature is available only if gdb was configured using --with-python.

You have to configure gdb using that option:


Where location is the location of python you would like to use gdb with.

share|improve this answer
add comment

I just ran into the similar situation when trying to debug Webkit:

$ python Tools/gdb/webkit.py
Traceback (most recent call last):
  File "Tools/gdb/webkit.py", line 38, in <module>
    import gdb
ImportError: No module named gdb

I then realized that this script should be invoked in gdb to make it working:

(gdb) source Tools/gdb/webkit.py
(gdb) p run
$1 = (const WebCore::TextRun &) @0x7fffffffa450: {m_characters = "Plugin Testsa", m_len = 12, m_xpos = 0, 
  m_padding = 0, m_allowTabs = false, m_rtl = false, m_directionalOverride = false, 
  m_applyRunRounding = true, m_applyWordRounding = true, m_disableSpacing = false}

Hope this helps.

share|improve this answer
add comment

Your Answer


