Search code examples
debuggingsshvagrantpycharmwinpe

Best way to debug Python for WinPE


I'm developing app to deploy Windows OS and some software with drivers in it. This application is intended to work in WinPE (Windows Preinstallation Environment), which I'm booting from USB stick for laptop or VHD for VM. For development I'm using IDE (PyCharm) installed on desktop Windows 7. But it's really uncomfortably to write code on desktop PC, further copy it on usb or VHD and run code on laptop or VM without possibility to debug it. I found out that Vagrant can help me to run code in VM, but it's impossible to install SSH right in WinPE.

Could anybody suggest anything else?


Solution

  • Found only such a solution: 1. Share your project directory to be sure that desktop and remote code are completely equal. 2. Make sure remote machine (or VM) can ping your desktop PC with PyCharm installed. 3. Follow PyCharm instructions for Remote debugging using its pydev.egg.

    It's not so convenient cause you should restart app on remote machine or VM every time when you want to debug app again. Also there is a bug in PyCharm Remote Debug: if you have cyrillic locale in desktop OS you probably get path mapping error. So for this moment I can't get it work with breakpoints properly.

    This is my code for debug:

    from os import system as sys_call
    import os
    import sys
    
    debug_egg_dir = "PyCharm\\debug-eggs\\"
    debug_egg_name = "pycharm-debug-py3k.egg"
    python_dir = os.path.dirname(sys.executable)
    
    PORT = 15999
    
    
    def connect(server):
        if sys_call("ping -n 1 " + server + ">nul") != 0:
            raise ConnectionError("PyCharm Debug Server ({}) is not available".format(server))
    
        try:
            sys.path.append(python_dir + "\\" + debug_egg_name)
            import pydevd
            pydevd.settrace(server, port=PORT, stdoutToServer=True, stderrToServer=True, suspend=False)
    
        except ImportError:
            raise FileNotFoundError("Copy file {0}{1} into {2}".format(
                debug_egg_dir,
                debug_egg_name,
                python_dir
            ))
    
        except Exception as e:
            raise e