Part 2 Visual Studio Code remote debugging
So I managed to get the Visual Studio Code remote debugging working as well on Windows.
I couldn’t get it to work on Mac, due to the fact that it refused to download a more recent version of the Python Extension than 2018.6.0 which has a bug that means it can’t connect. That might just be problem isolated to me, but be warned, first check your Python extension is up to date. I have also not tried Linux
So as I mentioned in my first post, you can get Visual Studio Code and set this up for free.
Setting it up
So I essentially followed these steps:
-
Open the app folder in VS Code. It must be the folder, not just the individual python file you want to debug.
-
Ensure the python extension is installed and uptodate:
-
Now add the remote debug configuration.
- The IP should be local host.
- The port doesn’t matter too much as long as it doesn’t clash with a different process.
- The localRoot and remoteRoot should be
"${workspaceFolder}"
, unless Toolkit is running the code from a different location, then the folder you opened in VS Code.
-
Download
ptvsd
. The easiest way I found of doing this, was to pip install it to my local python intepreter. But since we need to import it in something like Maya, which has its own python intepretter, I copied theptvsd
folder from thesite-packages
folder to a separate location. The reason I did this, is because it is never a good idea to sys.path.append the site-packages folder from a different interpreter, as some libraries might be compiled specifically for a given interpreter. Therefore you might see errors if Maya’s native Python interpreter libs clash with your system installed libs.
-
Now you can either add the following code to your app, or run it in the Software’s native python script editor:
import sys # Change the path to match where ever you copied the `ptvsd` package. sys.path.append("E:\\python_libs") import ptvsd # Allow other computers to attach to ptvsd at this IP address and port. ptvsd.enable_attach(address=('localhost', 5678), redirect_output=True) # Pause the program until a remote debugger is attached ptvsd.wait_for_attach()
-
Start the debugger in VS Code:
Now it should stop at any breakpoints you’ve added in your code!
Happy bug hunting!