Search code examples
python-2.7pycharmremote-debuggingmaya

PyCharm cannot catch breakpoints if the python code is running in Maya's Script Editor


I am debugging a Python code for Maya through the remote debugger in PyCharm. The remote debugger can catch breakpoints as expected if the code is run at the command line, but it fails to do that if the Python code is running inside Maya's Script Editor.

The Python code is running on a Ubuntu machine while the PyCharm remote debugger is running on Windows.

I launch Maya on the Ubuntu machine from the directory that contains the script. The path mapping of PyCharm is simply set to "." for the Windows path that contains the same python script. Can you help me with this problem? Thanks a lot.


Solution

  • OK, I have found the answer myself. I had thought the problem is related to path mapping settings, but now it turns out that it is related to how the python code is invoked.

    The pydevd, which communicates with the remote debugging service of PyCharm running on Windows, has to know what the whole python script is so that it can transmit this piece of information to PyCharm for it to find the source and corresponding breakpoints. It is currently not possible if we run the python script directly in Maya Script Editor by, say, clicking the "Load Script" button because Maya is not smart enough to let pydevd know that the whole stuff in the Script Editor is a complete python module file -- it is still inherently a line-wise python interpreter, only in a form of multi-line editor to give us some editing convenience.

    So the key is how to let pydevd know what the whole python module script is. This is done by running the code through import. It's not surprising that what is imported should be a whole module python code. As a result, after launching pydevd in Maya using

    import pydevd
    pydevd.settrace('the.remote.debugger.IP', port=7720, stdoutToServer=True, stderrToServer=True)
    

    , we should invoke the python code we want to debug by typing in Script Editor

    import theBuggyModule
    

    This way, after clicking the blue "Execute" icon, the Script Editor will stop respond, because the execution flow now transfers to the remote debugger of PyCharm. Sometimes PyCharm still has some difficulty in recognizing the source and therefore catching breakpoints. In this case, just click "Auto-detect" link in the newly created tab in PyCharm and the source file should be in the list. This is done only once. Note that this works only when the path map is set as described in my question. If not, you should properly set the mapping so that PyCharm can find the source in the remote machine where remote debugger service is running.

    I know my answer is not perfect so any (correct) complementary stuff is welcomed. Just don't say: "That's not possible", "You can only debug locally", "It doesn't work like that" or something like that. Thanks.