खोज…


पायथन डीबगर: स्टेप-थ्रू डीबगिंग विथ _pdb_

पायथन स्टैंडर्ड लाइब्रेरी में पीडीबी नामक एक इंटरैक्टिव डिबगिंग लाइब्रेरी शामिल है। pdb में व्यापक क्षमताएं होती हैं, जो आमतौर पर एक प्रोग्राम को 'स्टेप-थ्रू' करने की क्षमता के रूप में उपयोग की जाती हैं।

तुरंत स्टेप-थ्रू डीबगिंग उपयोग में प्रवेश करने के लिए:

python -m pdb <my_file.py>

यह प्रोग्राम की पहली पंक्ति में डिबगर शुरू करेगा।

आमतौर पर आप डिबगिंग के लिए कोड के एक विशिष्ट अनुभाग को लक्षित करना चाहेंगे। ऐसा करने के लिए हम pdb लाइब्रेरी को आयात करते हैं और इस परेशान उदाहरण कोड के प्रवाह को बाधित करने के लिए set_trace () का उपयोग करते हैं।

import pdb

def divide(a, b):
    pdb.set_trace()
    return a/b 
    # What's wrong with this? Hint: 2 != 3

print divide(1, 2)

इस कार्यक्रम को चलाने वाले इंटरैक्टिव डिबगर लॉन्च करेंगे।

python foo.py 
> ~/scratch/foo.py(5)divide()
-> return a/b
(Pdb) 

अक्सर इस कमांड का इस्तेमाल एक लाइन पर किया जाता है, इसलिए इसे सिंगल # कैरेक्टर के साथ कमेंट किया जा सकता है

 import pdf; pdb.set_trace()

(Pdb) प्रॉम्प्ट कमांड में दर्ज किया जा सकता है। ये कमांड डीबगर कमांड या अजगर हो सकते हैं। चर प्रिंट करने के लिए हम डीबगर, या अजगर के प्रिंट से पी का उपयोग कर सकते हैं।

(Pdb) p a
1
(Pdb) print a
1

सभी स्थानीय चरों के उपयोग की सूची देखने के लिए

locals

निर्माण समारोह

ये जानने के लिए अच्छे डिबगर कमांड हैं:

b <n> | <f>: set breakpoint at line *n* or function named *f*.
# b 3
# b divide
b: show all breakpoints.
c: continue until the next breakpoint.
s: step through this line (will enter a function).
n: step over this line (jumps over a function).
r: continue until the current function returns.
l: list a window of code around this line.
p <var>: print variable named *var*.
# p x
q: quit debugger.
bt: print the traceback of the current execution call stack
up: move your scope up the function call stack to the caller of the current function
down: Move your scope back down the function call stack one level
step: Run the program until the next line of execution in the program, then return control back to the debugger
next: run the program until the next line of execution in the current function, then return control back to the debugger
return: run the program until the current function returns, then return control back to the debugger
continue: continue running the program until the next breakpoint (or set_trace si called again)

डिबगर भी अंतःक्रियात्मक रूप से अजगर का मूल्यांकन कर सकता है:

-> return a/b
(Pdb) p a+b
3
(Pdb) [ str(m) for m in [a,b]] 
['1', '2']
(Pdb) [ d for d in xrange(5)]
[0, 1, 2, 3, 4]

ध्यान दें:

यदि आपका कोई चर नाम डिबगर कमांड के साथ मेल खाता है, तो विस्मयादिबोधक चिह्न का उपयोग करें ' ! 'वेरिएबल से पहले स्पष्ट रूप से वेरिएबल का संदर्भ लें न कि डिबगर कमांड का। उदाहरण के लिए, अक्सर ऐसा हो सकता है कि आप किसी काउंटर के लिए वैरिएबल नाम ' c ' का उपयोग करते हैं, और आप डिबगर में इसे प्रिंट करना चाह सकते हैं। एक साधारण ' सी ' कमांड अगले ब्रेकपॉइंट तक निष्पादन जारी रखेगा। इसके बजाय वेरिएबल के मान को प्रिंट करने के लिए ' c ' का उपयोग करें:

(Pdb) !c
4

IPython और ipdb के माध्यम से

यदि IPython (या Jupyter ) स्थापित हैं, तो डीबगर का उपयोग करके इनवॉइस किया जा सकता है:

import ipdb
ipdb.set_trace()

पहुंचने पर, कोड बाहर निकल जाएगा और प्रिंट होगा:

 /home/usr/ook.py(3)<module>()
      1 import ipdb
      2 ipdb.set_trace()
----> 3 print("Hello world!")

ipdb>

स्पष्ट रूप से, इसका मतलब है कि किसी को कोड को संपादित करना होगा। एक सरल तरीका है:

from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose',
                                     color_scheme='Linux',
                                     call_pdb=1)

यह डीबगर को बुलाया जाएगा यदि कोई अनकहा अपवाद उठाया गया है।

दूरस्थ डिबगर

कुछ बार आपको अजगर कोड को डीबग करने की आवश्यकता होती है जिसे किसी अन्य प्रक्रिया द्वारा निष्पादित किया जाता है और इस स्थिति में rpdb काम आता है।

rddb pdb के चारों ओर एक आवरण है जो एक सॉकेट हैंडलर के लिए स्टड और स्टडआउट को पुन: रूट करता है। डिफ़ॉल्ट रूप से यह पोर्ट 4444 पर डीबगर को खोलता है

उपयोग:

# In the Python file you want to debug.
import rpdb
rpdb.set_trace()

और फिर आपको इस प्रक्रिया से जुड़ने के लिए इसे टर्मिनल में चलाने की आवश्यकता है।

# Call in a terminal to see the output
$ nc 127.0.0.1 4444

और आपको pdb promt मिलेगा

> /home/usr/ook.py(3)<module>()
-> print("Hello world!")
(Pdb)


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow