Remote Maya Python Debugging in Eclipse
PyDev has debugger:
Turns out Pydev has a nice plugin/package calling the module pydevd.py. Instructions on generic usage are here:
http://pydev.org/manual_adv_remote_debugger.html
When we say remote, it doesn’t have to be on another computer, it can be on your own computer, it is just debugging a script that is not being run from within your Eclipse Development Environment, but is being executed from some other source like a Python IDLE console or a DCC app running Python, like Maya! So let’s do it . . .
Setup pythonpath:
Firstly, you’ll need the path to the pydevd.py module in your Maya session’s pythonpaths. Follow the instructions in the link above to find out where this path is (something like: eclipse/plugins/org.python.pydev.debug_x.x.x/pysrc/). Add it to your python path in via Maya.env, userSetup.mel, userSetup.py, or your preferred method. Restart Maya of course.
Start Pydev server:
Now you actually need to start the server per the link’s instructions by first switching to your debug perspective, ‘cause that’s what it’s for and then pressing the little bug icon in your toolbar to start the Pydev server.
Load module, call method:
You can now add these lines to the script you want to debug:
# basic start trace import pydevd pydevd.settrace()
Probably want to echo to Eclipse w/ these args:
# echo stdout and errors to Eclipse pydevd.settrace(stdoutToServer=True, stderrToServer=True, suspend=False)
If you run the script with this line in Maya when your Eclipse Pydev server is running, Eclipse will grab the process at this line. But what is nicer is to just establish the connection and set your breakpoint in any script in Eclipse and then Eclipse will grab the process as soon as you run that script in Maya. You’ll need to modify the suspend=True arg to False so suspension does not happen at the spot this line is run, but instead just makes the connection and waits for suspension to happen when a breakpoint is hit:
# just put this at the top of any script import pydevd pydevd.settrace(stdoutToServer=True, stderrToServer=True, suspend=False)
Set your breakpoint in your python script. Call it from Maya, Maya will hang, switch to Eclipse and look for the magic green line.
Now you get awesome stack data, listed as “Variables” in Eclipse and the Console is interactive! just like the debug probe in Wing. Don’t forget that Eclipse can ‘watch’ variables with the “Expressions” window too! If you don’t think this is awesome, you are not a serious Maya Python programmer, ‘cause this is the coolest thing since sliced bread if you ask me.
Eventually you’ll probably want to just make a command like this:
# better yet, make little method def connectToEclipse(): import pydevd pydevd.settrace(stdoutToServer=True, stderrToServer=True, suspend=False)
And just call the function whenever your gonna start your debugging.
One Thing Missing:
There is one major convenience missing in this “debug probe” however, and that is code-completion. One of the huge advantages of the Debug Probe in Wing IDE while you’re debugging Maya is that you can “see” what the current environment is inside Maya at any given moment. This is so handy it is hard to describe. Very handy for diagnosing why certain Python packages and modules are not importing so well. With Wing’s Debug Probe, you just interrupt your session, and start typing “import” and you can start to experiment with paths and namespaces to see what will import under what name. Anyway, I miss the code-completion in this Console.
Of course the connections can get a little sticky and you may have to restart one or both apps occasionally to get things setup again. Just FYI.
Infinitely Awesome:
Regardless, this connection is awesome; so infinitely valuable to debug in real-time, I won’t even start. Wing does it a little more nicely, but it does cost. Great thing about Eclipse and Pydev, is that it is free!
Oh. My. God.
I only started using Eclipse a couple weeks ago after using jEdit for years before (with MEL). Your GDC lecture convinced me to use PyMel (I had been previously undecided) and try out Eclipse, and it’s been a revelation. I can’t imagine ever going back.
I’m more of an artist than a tools guy, but enjoy both. I finally switched to python recently and decided to explore a real IDE. Anyway, I just can’t thank you enough, also to Christian Akesson, Nathan Horne, and and tech-artists.org, because that’s how I found my way here. Making computer graphics a little less painful and more fun is definitely appreciated 🙂
Thanks for letting us know man. That totally makes it all worth it.
Welcome to the next level!
Thanks for the guide! I wouldn’t have figured this out on my own.
After going hardcore Vim-gdb only, I came back to Eclipse for a school project.
I don’t love the interface, but the smoothness of debugging and autocompletion are hard to convince myself to live without.
😀
-Murphy
Thanks a lot !
How could I live without this tips ! It works perfect in Maya & Eclipse !
When I import pydevd in Maya 2014 I get the following warning.
# ——————————————————————————-
# pydev debugger: CRITICAL WARNING: This version of python seems to be incorrectly compiled (internal generated filenames are not absolute)
# pydev debugger: The debugger may still function, but it will work slower and may miss breakpoints.
# pydev debugger: Related bug: http://bugs.python.org/issue1666807
# ——————————————————————————-
Then when I run pydevd.settrace(), I get an error:
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\threading.py
Those warnings can be ignored. They happen everytime you connect with pydevd to mayapy.
Thanks very much, it seems to work but fails when it does “import maya.cmds as cmds” any idea?
pydev debugger: starting
Traceback (most recent call last):
File “C:\Eclipse\plugins\org.python.pydev_3.3.3.201401272249\pysrc\pydevd.py”, line 1738, in
debugger.run(setup[‘file’], None, None)
File “C:\Eclipse\plugins\org.python.pydev_3.3.3.201401272249\pysrc\pydevd.py”, line 1355, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File “C:\Projects\Eclipse\TowersOfHanoi\sphere.py”, line 6, in
import maya.cmds as cmds
ImportError: No module named maya.cmds
Thanks so much.
you need to make sure your sphere.py script can import maya.cmds. The debugger may have nothing to do with your pythonpath environment at this point, which is what this issue is. Get sphere.py working inside of Maya w/o the debugger connected first.
Thanks a lot jason. Now I have gone a step further. I ran the script you shared in the maya script editor having the pythonpath correctly set and it pops me these couple of lines:
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\threading.py
# pydev debugger: Unable to find real location for:
Thanks a lot for your help! If I can do this it must be wonderful!
You should be able to ignore those warnings. A whole spew of ’em fly out when you achieve connection, something like this:
# -------------------------------------------------------------------------------
# pydev debugger: CRITICAL WARNING: This version of python seems to be incorrectly compiled (internal generated filenames are not absolute)
# pydev debugger: The debugger may still function, but it will work slower and may miss breakpoints.
# pydev debugger: Related bug: http://bugs.python.org/issue1666807
# -------------------------------------------------------------------------------
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\threading.py
# pydev debugger: Unable to find real location for: C:\Program Files (x86)\JetBrains\PyCharm 3.0.1\pycharm-debug.egg\pydevd.py
# pydev debugger: Unable to find real location for: C:\Program Files (x86)\JetBrains\PyCharm 3.0.1\pycharm-debug.egg\pydevd_comm.py
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\logging\__init__.py
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\ntpath.py
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\genericpath.py
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\getpass.py
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\os.py
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\traceback.py
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\linecache.py
# pydev debugger: Unable to find real location for: C:\Program Files (x86)\JetBrains\PyCharm 3.0.1\pycharm-debug.egg\pydevd_io.py
INFO: Probably attached to PyCharm
# pydev debugger: Unable to find real location for:
# pydev debugger: Unable to find real location for: C:\Program Files\Autodesk\Maya2014\bin\python27.zip\weakref.py
# pydev debugger: Unable to find real location for:
# pydev debugger: Unable to find real location for: C:\Program Files (x86)\JetBrains\PyCharm 3.0.1\pycharm-debug.egg\pydevd_tracing.py
Can ignore this though. This signifies success!
hey jason, quick question… i’ve got this working with the settrace put in-line at the beginning of my script: when i ctrl+enter to send the script to maya, the debugger will take control of it and i can see all the stack info.
however, if i try to take the next step and set a break point, it doesn’t seem to catch correctly. i’m running your connectToEclipse method prior to attempting to debug and it is connecting fine, i’m also seeing data in the console as the debug server is up, so i am pretty sure that the connection is fine, but it doesn’t appear to be catching the breakpoint. this happens both when the settrace param ‘suspend’ is true and false. my process for running the script is run script in eclipse (send to maya), reload(module) in maya, then run the script. i have also tried simplifying this process by simply having a hello world test def and setting a break on the hello world line, and it also fails to catch (removing the need to reload the module basically). i’m sure this is user error somewhere along the way i’m just not sure how maya actually would handle breakpoints.. any tips?
Hey Jason,
Awesome information you put out there, thanks so much. I ran into a Problem with Debugging my PySide tools for maya 2015 with eclipse and the pydevd debugging server.
seems like everytime i try to get the maya mainWindow with . OpenMayaUI.MQUtil.mainWindow()
The PythonInterpreter stops working.
Is it even possible to debug scripts which make use of PySide Qt Windows with eclipse?
I’m quite new to this, so I’m sorry if I’m missing something really obvious.
thanks in advance,
Martin
I think it is possible, yes. I’ve been using Pycharm for years now, but it also uses pydevd debugging server. I haven’t hooked up PySide recently but I will probably need to somewhat soon. When I do, I’ll confirm and post here.
I’ve managed to setup everything in Eclipse (PyDev, PyMEL-autocompletion, syntax highlighting, “Maya-Eclipse connect” -eclipse plugin and the debugger (PyDevD). The path to PyDev has also been added to my PYTHONPATH in Maya.env (confirmed by running import pydevd in Maya).
However, breakpoints doesn’t work at all. Neither the green points you add next to the line number in Eclipse, nor the pydevd.settrace() -cmd.
Unfortunately I haven’t spent any time in Eclipse since I found PyCharm. Leaps better than PyDev in Eclipse even. Give it a shot and it may resolve those lingering issues.