When using IBM Open Enterprise SDK for Python, developing your Python application with Visual Studio Code offers many advantages. Since this is running under Unix System Services on z/OS, you’ll need to be connecting remotely to the system so that you can both modify your files and remotely debug the application. So how can you do this?
There’s a Python package called debugpy which allows for remote debugging. The way that it works is that it launches your Python application and listens for any debuggers that attempt to connect to it. We can use an extension for Visual Studio Code to connect and then debug your application. Note that this should only be done in secure network environments, as anyone who knows the port can connect and execute arbitrary code.
Installing & Setting up Prerequisites for Remote Debugging
There’s two parts that need to be installed:
- Install the Python package debugpy on your z/OS system.
- This package can be obtained from the Python AI Toolkit for IBM z/OS, which comes with optional support from ShopZ.
- Alternatively, you can run
pip3 install debugpy to install this from PyPI.
- Install the Visual Studio Code extension Python Debugger.
- Add a configuration in Visual Studio Code for the Python Debugger. This has 3 key sections which need to be included – the hostname or ip of the remote z/OS machine you are connecting to, the port its listening on, and a mapping between where the sources can be found on both the local machine & remote machine so that Visual Studio Code is able to correctly detect when files are the same and set break points.
Once this has been finished, you’re ready to begin debugging.
Remotely Debugging
To launch the debugging server with Python on z/OS, you can run the following:
python3 -m debugpy --listen 0.0.0.0:5001 --wait-for-client application.py
The --listen flag specifies the ip and port that it’s listening on
The --wait-for-client specifies that it should wait for a remote debugger to connect before starting to run the application. You may consider omitting this.
Within Visual Studio Code, you can now use the Python Debugger to remotely connect, set breakpoints on the local copy, view variables and stack trace. One problem with our current setup is that debugging requires a copy of the Python script both on your local machine and on z/OS. To fix that, we can use a Visual Studio Code extension called Zowe Explorer. This extension allows us to modify files on z/OS Unix or datasets, and submit & view jobs, and specifically allows us to set up a Visual Studio Code workspace where we can remotely modify our Python scripts and debug them. Note that while it is called Zowe Explorer, it does not require Zowe to be installed on your z/OS system – there is an additional plugin to allow z/OS Unix access with ftp behind the covers.
Installing & Setting up Zowe Explorer
You can install & setup Zowe Explorer by doing the following steps:
1) Install the Zowe Explorer Visual Studio Code extension
2) (Optional) Install the Zowe Explorer for IBM z/OS FTP Visual Studio Code extension
3) Set up a new Zowe profile by navigating to the Zowe Explorer extension on the activity bar. Under the global_base -> host tab, add your z/OS hostname and modify any ports required to connect.
4) Under the Unix System Services tab, click the + and add a new profile for zftp, and connect it to the directory that has the Python application that you wish to edit & debug.
Editing z/OS Unix files & debugging
You can now modify any file on z/OS Unix using Zowe Explorer. You can command/right click on any of the directories and add them to your workspace. However, to be able to debug them, you’ll need to update the path mappings for your local/remote files using Zowe Explorer’s custom URI. For example, if you wanted to remotely debug an application located at the following path on your z/OS system:
/home/pitman/debug_example
You could specify the following as your pathMapping:
"localRoot": "zowe-uss:/zftp/home/pitman/debug_example",
"remoteRoot": "/home/pitman/debug_example”
In this example, zowe-uss: is the custom URI being used.
/zftp/ Is the profile name being used.
/home/pitman/debug_example is the actual full path on the z/OS system