DEBUG只有一个函数(或模块),在Pycharm中运行程序的其余部分?

时间:2016-01-14 12:10:37

标签: python debugging 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()。

有没有办法在Pycharm中运行long_one()和DEBUG short_one()? 换句话说,有一种方法可以执行:

  1. DEBUG,规范long_one()应在RUN模式下处理
  2. 或RUN是否应该调试short_one()的规范?

2 个答案:

答案 0 :(得分: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的输出

doing the release version

当我“调试”它时来自PyCharm的输出

doing the debug version

答案 1 :(得分:0)

Asagen建议:

  1. 将调试器附加到python控制台。
  2. 以RUN模式启动我的脚本
  3. 在脚本运行时我做了int main() { FILE *fptr; string fileName; cout<<"Please provide the name of the file: "<<endl; cin>>fileName; fileName = "Test Images/"+fileName+".bmp"; fptr = fopen(fileName.c_str(), "rb"); cout<<"FileName : "<<fileName<<endl; if(fptr == NULL){ cout<<"File Not found"<<endl; getch(); return -1; } unsigned char total[54]; fread(total, sizeof(unsigned char), 54, fptr); cout<<endl; cout<<"Type: "<< *(char*)&total[0x00]<<*(char*)&total[0x01] <<endl; cout<<"size: "<< *(int*)&total[0x02]<<endl; cout<<"offset: "<< *(int*)&total[0x0A]<<endl; cout<<"Info header size: "<< *(int*)&total[0x0E]<<endl; cout<<"Bitmap width: "<< *(int*)&total[0x12]<<endl; cout<<"Bimap height "<< *(int*)&total[0x16]<<endl; cout<<"Color planes: "<< *(int*)&total[0x1A]<<endl; cout<<"BPP: "<< *(short int*)&total[0x1C]<<endl; cout<<"Compression Method "<< *(int*)&total[0x1E]<<endl; cout<<"Raw bmp data size: "<< *(int*)&total[0x22]<<endl; cout<<"Horizontal Resolution: "<< *(int*)&total[0x26]<<endl; cout<<"Vertical Resolution: "<< *(int*)&total[0x2A]<<endl; cout<<"No of Color: "<< *(int*)&total[0x2E]<<endl; cout<<"No of imp colors: "<< *(int*)&total[0x32]<<endl; fclose(fptr); getch(); return 0; } 并选择了我的流程。
  4. 调试器从我这样做的那一刻开始,并在它遇到的第一个断点处停止。

    有一个不便之处 - 我必须知道何时开始调试(在哪个时刻将调试器连接到进程)。我提出了一个解决方法:

    1. 在代码中添加一个想要开始调试的infininte循环(见下文):
    2. Tools/Attach to Process
      1. 在while循环中添加断点
      2. 在运行过程中,当您在控制台中看到打印文本“好人,现在是时候开始调试了!”时,请附加调试程序进行处理。
      3. 接下来当它在无限循环中停止时,评估代码片段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 ,以便离开循环
      4. 就是这样,之前运行完整代码后你现在处于DEBUG模式,

        如果要返回RUN模式,只需停止调试即可。可以在RUN和DEBUG之间多次和在任何你想要的地方切换