2016-01-14 20 views
0

我會盡量解釋簡單的示例程序,我的問題(我的問題要複雜得多,因爲我的程序要複雜得多)。DEBUG只有一個函數(或模塊),運行其餘的程序在Pycharm中?

讓假設我有具有2行的程序,使2個功能:

data = long_one() #takes 2 hours in DEBUG mode, 15min in RUN mode 
short_one(data) #i want to DEBUG this one 

讓也不能不說是非常難以製備data變量和獲得它是通過運行的唯一方式函數long_one()。

是否有一種方式來運行long_one()和調試short_one()在Pycharm? 換言之是有以執行任一方式:

  1. DEBUG與規範,long_one()應在RUN模式
  2. 或RUN與規範,short_one()應當調試處理?
+0

在python控制檯中有一個用於附加調試器的按鈕。這是我通常使用的。我還沒有嘗試過,但也許你可以在運行/編輯配置中標記「之後顯示命令行」,然後使用tools/attach_to_process。如果你附加一個調試器,你將不得不導入short_one – Sumido

+0

@Asagen那麼,雖然它不是我的問題的答案,但它是有保證地解決我的問題!有一個不便之處,你必須知道什麼時候開始調試,但我有一個解決方法(在我的答案中的描述) – dankal444

回答

0

由於Asagen提出時,我當我 「跑」 它

doing the release version 

從PyCharm輸出:

  1. 將調試器附加到python c onsole。
  2. 在運行模式下啓動我的腳本
  3. 腳本運行時我做了Tools/Attach to Process並選擇了我的過程。

調試器已經從我做這件事的那一刻開始,停在它遇到的第一個斷點處。


有一個不便 - 我必須知道什麼時候開始調試(在哪個時刻附加調試器來處理)。我提出了一個解決辦法:

  1. 添加到代碼代替infininte循環要開始調試(見下文):

data = long_one() # takes 2 hours in DEBUG mode, 15min in RUN mode 
infinite_loop = True 
print "OK man, it is the time to start debugging!" 
while infinite_loop: 
    time.sleep(0.2) # add breakpoint here 
short_one(data) #i want to DEBUG this one 

  • 添加斷點while循環
  • 內部在運行過程中,當您在控制檯中看到印文「確定的人,它是T ime開始調試!「,附加調試程序進行處理。
  • 下,當它在無限循環停止,評估代碼片段infinite_loop = False,所以你離開循環
  • 這是它,你現在在調試模式下運行前整個代碼後,

    如果您想要回到RUN模式,只需停止調試器。可以在RUN和DEBUG之間切換多次,並在任何你想要的地方

    0
    import sys 
    
    def do_the_thing_for_debug(): 
        print('doing the debug version') 
    
    def do_the_thing(): 
        print('doing the release version') 
    
    if __name__ == '__main__': 
        """ 
        Detecting if you're in the PyCharm debugger or not 
        WARNING: This is a hacky & there is probably a better 
          way of doing this by looking at other items in the 
          global() namespace or environment for things that 
          begin with PyCharm and stuff. 
          Also, this could break in future versions of PyCharm! 
        If you put a breakpoint HERE and look at the callstack you will 
        see the entry point is in 'pydevd.py' 
        In debug mode, it copies off sys.argv: "sys.original_argv = sys.argv[:]" 
        We abuse this knowledge to test for the PyCharm debugger. 
        """ 
        if hasattr(sys, 'original_argv'): 
         do_the_thing = do_the_thing_for_debug 
    
        do_the_thing() 
    

    從PyCharm輸出 「調試」 它

    ​​
    +0

    由於代碼表明它是「檢測你是否在PyCharm調試器」。我的問題是如何「混合」DEBUG和RUN模式。 – dankal444

    相關問題