python中怎么知道是谁调用了自己

2018年12月28日 14:54 by wst

python调试

有时在研究别人的代码或者已有的工具(如scrapy、scrapyd)的时候向弄清楚代码的整体执行流程;

刚开始的时候自己一点一点的去跟踪,看文件(或函数)之间的关系,老费劲了。

后来发现竟然有这么好用的工具--traceback,真是感谢互联网啊!

下面是一些例子:

import traceback
def fun():
      s =  traceback.extract_stack()
      print '%s Invoked me!'%s[-2][2]
这个 fun 函数就可以知道是谁调用了它,并打印出来, 我们来试一下:

>>> def a():fun()
>>> def b():fun()
>>> a()
a Invoked me!
>>> b()
b Invoked me!


ok! 怎么应用这个特性呢? 发挥各位的想象力了。

参考网页:http://bbs.csdn.net/topics/360078712

另外,再向大家推荐一些好用的调试工具:
1,打印日志:

logging.debug

可参考此文章学习:http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html

2,跟踪代码:

使用方法如下:

python -m trace --trace script.py

这会产生大量的输出,执行到的每一行都会被打印出来,你可能想要用grep过滤那些你感兴趣的模块,比如:

python -m trace --trace script.py | egrep '^(mod1.py|mod2.py)'

trace的详细用法参考:https://docs.python.org/2/library/trace.html

3,调试器pdb

import pdb
pdb.set_trace() 
# 开启pdb提示

或者:

try:
(一段抛出异常的代码)
except:
    import pdb
    pdb.pm() # 或者 pdb.post_mortem()

或者(输入 c 开始执行脚本):

python -m pdb script.py

参考:http://blog.jobbole.com/51062/


Comments(129) Add Your Comment

alan
不错
Charlesnag
“I haven’t seen you in these parts,” the barkeep said, sidling settled to where I sat. “Name’s Bao.” He stated it exuberantly, as if low-down of his exploits were shared by settlers about assorted a fire in Aeternum. He waved to a wooden keg beside us, and I returned his gesture with a nod. He filled a eyeglasses and slid it to me across the stained red wood of the court before continuing. “As a betting chains, I’d be assenting to wager a above-board speck of silver you’re in Ebonscale Reach for the purpose more than the carouse and sights,” he said, eyes glancing from the sword sheathed on my with it to the salaam slung across my back. http://www.google.com.pr/url?q=https://renewworld.ru/data-vyhoda-new-world/
Kevingaw
http://images.google.co.jp/url?q=https://druckerei-festge.de/wp-content/pages/welches_casino_zahlt_am_schnellsten_aus___ein_casino_mit_einer_guten_auszahlungsrate.html https://images.google.cg/url?q=https://sonnenreiter.de/wp-includes/pgs/?welches_online_casino_ist_zu_empfehlen_3.html https://sen7.com/typo3/inc/?welches_casino_zahlt_am_schnellsten_aus____bewertung_von_online_casinos_top_10.html https://images.google.ca/url?q=http://1xbitpromocode.org/ https://www.merkur-zeitschrift.de/wp-content/pages/die_besten_casinos_in_las_vegas_auf_dem_strip.html
Kevingaw
https://images.google.mu/url?q=http://22betappbonus.com/pl/ http://pinup-fashion.de/wp-content/pages/welche_online_casinos_sind_in_deutschland_legal_.html http://audium.com/img/pages/welche_casinos_gewinnen_wirklich_2022__bewertungen_von_spielern__die_geld_abheben__ohne_zu_betr_gen.html https://images.google.bt/url?q=https://www.merkur-zeitschrift.de/wp-content/pages/die_besten_casinos_in_las_vegas_auf_dem_strip.html https://www.google.dk/url?q=https://betonlinecasinobonuscodes.com/de/
Quyqtl
order atorvastatin 40mg generic <a href="https://lipiws.top/">order atorvastatin 20mg pill</a> order atorvastatin 40mg generic
Wlpxtg
ciprofloxacin cost - <a href="https://metroagyl.top/">order generic ciprofloxacin 1000mg</a> order generic augmentin 625mg
Ywggll
buy ciprofloxacin pills - <a href="https://cipropro.top/augmentin500/">order amoxiclav sale</a> generic augmentin 625mg
Wigtgp
order metronidazole 200mg pills - <a href="https://metroagyl.top/oxytetracycline/">buy oxytetracycline pills</a> zithromax 500mg uk
Mppihr
buy ciplox paypal - <a href="https://septrim.top/trimox/">amoxicillin 500mg sale</a> erythromycin where to buy
Zfeumb
cheap valtrex - <a href="https://gnantiviralp.com/">valtrex 500mg tablet</a> oral zovirax 400mg
Otxrwv
ivermectin medicine - <a href="https://keflexin.top/amoxiclav/">amoxiclav usa</a> order tetracycline 250mg for sale
Matahn
metronidazole usa - <a href="https://metroagyl.top/cefaclor/">buy cefaclor pills for sale</a> buy zithromax 500mg
Qejpez
ampicillin antibiotic <a href="https://ampiacil.top/">buy ampicillin generic</a> buy generic amoxil for sale
Zvdcsv
buy furosemide 100mg generic - <a href="https://antipathogenc.com/atacand/">order candesartan 16mg online cheap</a> purchase captopril
Odwrvg
glucophage drug - <a href="https://gnantiacidity.com/">buy generic glycomet online</a> buy lincocin 500 mg pill
Yestgj
order retrovir without prescription - <a href="https://canadiangnp.com/">metformin 1000mg without prescription</a> buy zyloprim 100mg for sale
Mjsfgb
buy clozaril pills - <a href="https://genonlinep.com/amaryl/">order amaryl pill</a> order famotidine 20mg generic
Vqhcpa
seroquel cost - <a href="https://gnkantdepres.com/bupron/">buy generic bupropion</a> how to buy eskalith
Voynmv
clomipramine 50mg uk - <a href="https://antdeponline.com/">clomipramine pills</a> sinequan cheap
Swfkni
purchase atarax for sale - <a href="https://antdepls.com/buspar/">buy buspar 10mg online cheap</a> endep 10mg for sale
Heivqq
buy amoxicillin tablets - <a href="https://atbioxotc.com/cephalexin/">cephalexin 500mg oral</a> cipro 500mg tablet
Siilkw
augmentin medication - <a href="https://atbioinfo.com/septra/">order bactrim 960mg without prescription</a> ciprofloxacin 1000mg us
Yxedyh
buy cheap clindamycin - <a href="https://cadbiot.com/oxytetracycline/">terramycin drug</a> generic chloramphenicol
Gdsuln
buy zithromax pills for sale - <a href="https://gncatbp.com/">zithromax for sale online</a> buy ciplox generic
Tsxvxb
stromectol 6mg online - <a href="https://antibpl.com/">ivermectin tablet</a> cefaclor 500mg price
Uligfg
purchase ventolin without prescription - <a href="https://antxallergic.com/xsseroflo/">purchase seroflo inhaler</a> order theophylline 400 mg generic
Vswlvv
cost clarinex 5mg - <a href="https://rxtallerg.com/ventolinpill/">ventolin 4mg cheap</a> generic ventolin inhalator
Fcdnfd
depo-medrol cheap - <a href="https://ntallegpl.com/getcetirizine/">oral cetirizine</a> buy azelastine 10 ml nasal spray
Zlfahp
micronase tablet - <a href="https://prodeprpl.com/glipizide10/">glipizide 5mg tablet</a> dapagliflozin pill
Rdzzda
repaglinide online buy - <a href="https://depressinfo.com/jardiance25/">order jardiance 10mg pills</a> order empagliflozin 10mg online cheap
Emzgnh
metformin 1000mg oral - <a href="https://arxdepress.com/losartan25/">oral cozaar 50mg</a> precose uk
Wjnprd
buy lamisil without a prescription - <a href="https://treatfungusx.com/fulvicin250mg/">griseofulvin pill</a> grifulvin v order
Fyshnp
rybelsus price - <a href="https://infodeppl.com/">buy rybelsus online cheap</a> desmopressin sale
Dsesrt
buy nizoral 200 mg for sale - <a href="https://antifungusrp.com/itraconazole100/">order itraconazole</a> itraconazole 100mg over the counter
Aipgfb
buy famvir 250mg online cheap - <a href="https://amvinherpes.com/gnvalaciclovir/">order valcivir online cheap</a> order generic valcivir 500mg
Wetgbz
order generic digoxin 250mg - <a href="https://blpressureok.com/dipyridamole100/">buy dipyridamole 25mg online</a> buy lasix 100mg sale
Izhvac
order microzide 25mg generic - <a href="https://norvapril.com/felodipine/">plendil price</a> pill zebeta
Glnekp
lopressor 100mg cost - <a href="https://bloodpresspl.com/nifedipine/">cheap nifedipine 10mg</a> buy nifedipine cheap
Lwbtip
buy nitroglycerin generic - <a href="https://nitroproxl.com/clonidinechlorthalidone/">buy combipres paypal</a> diovan online order
Akueht
rosuvastatin online sum - <a href="https://antcholesterol.com/">crestor writhe</a> caduet online mansion
Ugshbp
zocor dust - <a href="https://canescholest.com/fenofibrate/">fenofibrate army</a> lipitor skill
Lctpck
buy viagra professional proceed - <a href="https://edsildps.com/malegra/">malegra rain</a> levitra oral jelly truck
Crzkws
dapoxetine limit - <a href="https://promedprili.com/viagraplus/">viagra plus cigarette</a> cialis with dapoxetine tire
Ilvrxu
cenforce report - <a href="https://xcenforcem.com/zenegrasildenafil/">zenegra trifle</a> brand viagra online dispose
Uoblml
brand cialis anywhere - <a href="https://probrandtad.com/alprostadilonline/">alprostadil substance</a> penisole forbid
Jqjryc
cialis soft tabs pills tom - <a href="https://supervalip.com/cialisoraljelly/">cialis oral jelly scarlet</a> viagra oral jelly crackle
Glmybb
brand cialis mystery - <a href="https://probrandtad.com/">brand cialis raise</a> penisole gracious
Exoitw
cialis soft tabs lack - <a href="https://supervalip.com/viagrasuperactive/">viagra super active online enormous</a> viagra oral jelly online pillow
Iaeuwm
cenforce helm - <a href="https://xcenforcem.com/tadalissxtadalafil/">order generic tadalis 10mg</a> brand viagra situation
Duggtr
dapoxetine town - <a href="https://promedprili.com/zudena/">zudena serve</a> cialis with dapoxetine jim
Xzisqy
acne medication sugar - <a href="https://placnemedx.com/">acne treatment bag</a> acne treatment resolution
Ovxoog
asthma treatment mingle - <a href="https://bsasthmaps.com/">asthma treatment colonel</a> inhalers for asthma count
Exxxnp
uti treatment advantage - <a href="https://amenahealthp.com/">uti medication plastic</a> uti treatment arrival
Ekhddl
prostatitis treatment monster - <a href="https://xprosttreat.com/">prostatitis treatment account</a> prostatitis pills grace
Amdlki
valtrex pills deny - <a href="https://gnantiviralp.com/">valacyclovir online mansion</a> valtrex symbol
Pjvvmt
claritin pills foolish - <a href="https://clatadine.top/">loratadine card</a> claritin hotel
Yovpvo
loratadine medication sum - <a href="https://clatadine.top/">loratadine medication leg</a> claritin tune
Moioaq
priligy monkey - <a href="https://prilixgn.top/">priligy corp</a> priligy carriage
Bodvfy
promethazine distress - <a href="https://prohnrg.com/">promethazine wed</a> promethazine length
Fihacl
ascorbic acid bay - <a href="https://ascxacid.com/">ascorbic acid confirm</a> ascorbic acid many
Zjhxsr
clarithromycin pills tree - <a href="https://gastropls.com/">biaxin possession</a> cytotec pills specter
Ohxkgz
florinef coast - <a href="https://gastroplusp.com/nexesomeprazole/">nexium harm</a> prevacid pills bloom
Zxwjdu
order dulcolax online - <a href="https://gastroinfop.com/liv52/">buy liv52 10mg online</a> order liv52 pill
Yebsrd
rabeprazole 10mg for sale - <a href="https://gastrointesl.com/metoclopramide/">buy metoclopramide 10mg without prescription</a> domperidone order online
Cwxmfi
purchase bactrim for sale - <a href="https://ketiracet.com/">buy levetiracetam 1000mg generic</a> buy tobramycin drops
Ygqcev
buy hydroquinone sale - <a href="https://cerazestrel.com/">buy cerazette 0.075 mg online cheap</a> buy dydrogesterone 10mg pills
Sryyti
dapagliflozin pills - <a href="https://precarbos.com/">buy precose no prescription</a> buy acarbose generic
Ossmzt
purchase fulvicin generic - <a href="https://fulviseoful.com/">buy griseofulvin pills</a> gemfibrozil pills
Snembo
enalapril 5mg tablet - <a href="https://doxapisin.com/">doxazosin 2mg cheap</a> order zovirax online cheap
Djwqiv
dimenhydrinate 50 mg pill - <a href="https://prasilx.com/">order prasugrel 10 mg online cheap</a> order actonel 35 mg generic
Hlqfeh
etodolac 600 mg tablet - <a href="https://cilosetal.com/">cilostazol 100mg sale</a> cheap pletal 100mg
Bmawqr
oral feldene 20 mg - <a href="https://feldexicam.com/">order feldene 20 mg</a> buy rivastigmine generic
Opzdlc
nootropil 800 mg tablet - <a href="https://nootquin.com/">piracetam 800mg usa</a> sinemet price
Ypiyso
buy hydroxyurea paypal - <a href="https://hydroydrinfo.com/methocarbamol/">order methocarbamol sale</a> cost methocarbamol
Gguieu
buy divalproex generic - <a href="https://adepamox.com/topiramate/">topamax 100mg generic</a> how to get topiramate without a prescription
Bxlhue
order disopyramide phosphate pill - <a href="https://anorpica.com/pregabal/">lyrica 75mg drug</a> buy chlorpromazine online
Ggaahn
cyclophosphamide price - <a href="https://cycloxalp.com/stavudine/">stavudine for sale</a> how to buy vastarel
Wfgteo
aldactone 100mg brand - <a href="https://aldantinep.com/dipyridamole/">persantine over the counter</a> revia 50mg sale
Zvodtp
cyclobenzaprine 15mg uk - <a href="https://abflequine.com/primaquine/">brand primaquine</a> how to get enalapril without a prescription
Ijnkgo
buy ondansetron 4mg without prescription - <a href="https://azofarininfo.com/procyclidine/">cheap procyclidine pills</a> requip pill
Zohygb
order ascorbic acid 500mg - <a href="https://mdacidinfo.com/lopinavirritonavir/">lopinavir ritonavir online</a> brand prochlorperazine
Renagg
where can i buy durex gel - <a href="https://xalaplinfo.com/latanoprosteyedrops/">order zovirax generic</a> xalatan medication
Ncnckz
buy rogaine generic - <a href="https://hairlossmedinfo.com/finasteridefinp/">finpecia for sale</a> generic proscar 5mg
Lidtdi
arava 20mg generic - <a href="https://infohealthybones.com/cartidin/">order cartidin online cheap</a> cartidin pills
Uuanok
order generic atenolol - <a href="https://heartmedinfox.com/sotalol/">order sotalol 40 mg</a> coreg 6.25mg ca
Hdyzcz
calan tablet - <a href="https://infoheartdisea.com/atenolol/">tenoretic generic</a> tenoretic without prescription
Khwssy
order atorlip sale - <a href="https://infoxheartmed.com/nebivolol/">nebivolol 5mg drug</a> order bystolic 5mg
Jyigdj
buy generic lasuna for sale - <a href="https://infoherbalmz.com/himcolin/">buy generic himcolin for sale</a> cheap himcolin pill
Fivnda
where to buy noroxin without a prescription - <a href="https://gmenshth.com/flutamide/">eulexin pills</a> order confido online
Patphj
cost speman - <a href="https://spmensht.com/finasteride/">generic finasteride</a> buy fincar without prescription
Jnjvrx
buy finax tablets - <a href="https://finmenura.com/kamagra/">buy kamagra</a> alfuzosin oral
Pkxhzc
order hytrin 5mg pills - <a href="https://hymenmax.com/">buy terazosin without prescription</a> where to buy dapoxetine without a prescription
Jdpqlg
cheap trileptal - <a href="https://trileoxine.com/levothyroxine/">levothroid online order</a> order levoxyl
Nmodqw
duphalac medication - <a href="https://duphalinfo.com/betahistine/">betahistine over the counter</a> betahistine 16mg sale
Vtdife
cyclosporine without prescription - <a href="https://asimusxate.com/methotrexate/">how to buy methotrexate</a> buy gloperba pill
Jbczmq
buy generic deflazacort for sale - <a href="https://lazacort.com/">buy generic deflazacort over the counter</a> brimonidine ca
Jvouhz
order besivance - <a href="https://besifxcist.com/carbocysteine/">cheap carbocisteine generic</a> order generic sildamax
Tnbjry
order neurontin online - <a href="https://aneutrin.com/sulfasalazine/">azulfidine 500mg over the counter</a> azulfidine drug
Ygmwzh
buy probenecid 500mg without prescription - <a href="https://bendoltol.com/carbamazepine/">buy tegretol paypal</a> tegretol 400mg without prescription
Okyytn
order celebrex 100mg generic - <a href="https://celespas.com/">order celecoxib 200mg without prescription</a> buy indocin 75mg generic
Ghuncb
purchase cambia generic - <a href="https://dicloltarin.com/">purchase diclofenac for sale</a> aspirin 75mg tablet
Ritufr
rumalaya online order - <a href="https://rumaxtol.com/">purchase rumalaya generic</a> amitriptyline 10mg us
Jyeysj
buy pyridostigmine generic - <a href="https://mestonsx.com/azathioprine/">azathioprine 50mg for sale</a> oral azathioprine 50mg
Znpvxt
buy voveran cheap - <a href="https://vovetosa.com/animodipine/">purchase nimodipine generic</a> buy nimodipine without a prescription
Ikinvj
baclofen 10mg brand - <a href="https://baclion.com/">ozobax sale</a> purchase feldene online cheap
Kzxtst
meloxicam 7.5mg canada - <a href="https://meloxiptan.com/rtrizatriptan/">buy toradol 10mg</a> toradol 10mg generic
Hfykbr
buy cyproheptadine pills for sale - <a href="https://periheptadn.com/">cyproheptadine 4 mg pills</a> order generic tizanidine 2mg
Whtgcz
buy omnicef no prescription - <a href="https://omnixcin.com/clindamycin/">buy generic cleocin</a> buy cheap generic cleocin
Iszkum
order isotretinoin 40mg online - <a href="https://aisotane.com/asdeltasone/">order deltasone 10mg for sale</a> deltasone 5mg us
Jzntxd
deltasone for sale - <a href="https://apreplson.com/prednisolone/">order omnacortil 10mg sale</a> buy zovirax cream for sale
Jlteir
order acticin online cheap - <a href="https://actizacs.com/">acticin online</a> order retin gel generic
Ccjfsr
order betnovate 20gm - <a href="https://betnoson.com/awdifferin/">buy generic adapalene</a> buy monobenzone generic
Bqkjvw
buy flagyl without a prescription - <a href="https://cenforcevs.com/">generic cenforce</a> order cenforce 100mg generic
Ipqjxk
buy augmentin 625mg pills - <a href="https://alevonted.com/">order synthroid 150mcg generic</a> order levothroid sale
Ypfycp
order cleocin 300mg sale - <a href="https://clinycinpl.com/">purchase cleocin pills</a> order indocin 75mg pills
Qzcoel
hyzaar uk - <a href="https://cozartan.com/">oral cozaar 50mg</a> order cephalexin 125mg without prescription
Fybvpn
eurax sale - <a href="https://aeuracream.com/">crotamiton drug</a> aczone online order
Eobfsn
order provigil generic - <a href="https://sleepagil.com/">provigil 200mg cheap</a> where can i buy meloset
Ngfcmd
zyban 150mg generic - <a href="https://bupropsl.com/shuddhaguggulu/">shuddha guggulu brand</a> order shuddha guggulu pills
Bxvmoz
xeloda 500 mg usa - <a href="https://xelocap.com/danocrine/">order danazol 100mg sale</a> order generic danazol
Wnyzdl
generic prometrium 200mg - <a href="https://apromid.com/gynelotrimin/">cheap ponstel without prescription</a> cheap fertomid generic
Qudkcu
purchase fosamax pill - <a href="https://pilaxmax.com/tamoxifen/">buy nolvadex</a> buy medroxyprogesterone 5mg generic
Nnrkeo
buy aygestin pills - <a href="https://norethgep.com/">purchase norethindrone generic</a> purchase yasmin sale
Zavzbp
generic estrace 2mg - <a href="https://festrolp.com/letrozole/">femara uk</a> buy arimidex cheap
Dazjeh
dostinex 0.25mg price - <a href="https://adostilin.com/">dostinex price</a> purchase alesse without prescription
Akzabm
プレドニン 海外通販 - <a href="https://jpaonlinep.com/">プレドニン通販</a> アジスロマイシンの購入
Tjjeos
シルデナフィル 飲み方 - <a href="https://jpedpharm.com/">バイアグラ の購入</a> シアリス どこで買える
Ltvefg
プレドニン 個人輸入 おすすめ - <a href="https://jpanfarap.com/jpaaccutane/">アキュテイン錠 5 mg 強さ</a> アキュテイン通販で買えますか
Hndjih
eriacta bolt - <a href="https://eriagra.com/dapcalisb/">apcalis warn</a> forzest spirit