Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Meigyoku-Thmn/493bdfdb9128141109b8863cce32710f to your computer and use it in GitHub Desktop.
Save Meigyoku-Thmn/493bdfdb9128141109b8863cce32710f to your computer and use it in GitHub Desktop.
How to debug Renpy's python code by using remote debugging

Update 2021: This can only work with Ren'Py 7.3, the newer versions use a vastly different type of built-in Python interpreter that are completely incompatible with any canonical Python interpreter.


The problem is that the Python runtime in RenPy is a stripped one. It's not the full Python. Don't even try to replace it with a full python runtime, you would have a really bad time.

The only feasible way is to use remote debugging. I assume you use Windows OS and have knowledge about programming, file system.


Demonstration Video <Will add later>


First, download and install a Python runtime that is the same version as the Python runtime which RenPy uses, this will be used to supply missing modules (You don't have to remove your existing python, just install this at some place and don't let it modifies PATH or any env).

  • How to check RenPy's python version:
>> <Path_To_Your_RenPy's_Python>/python -V

For example:

>> C:/renpy-7.3.2/lib/windows-i686/python -V
Python 2.7.10

We will call this external Python runtime as PythonS


Install the "ptvsd" module for PythonS:

>> <path_to_PythonS>/python -m pip install ptvsd

For example:

>> C:/Python27/python -m pip install ptvsd

Modify the renpy.py file in your RenPy installation, add the following lines after existing import lines:

try:
   if ('use_debug' in os.environ and os.environ['use_debug'] == 'true'):
      externalPythonPath = "C:/Python27/python.exe" # replace this with your PythonS path
      from subprocess import check_output
      output = check_output([externalPythonPath, "-c", "import sys; print(sys.path)"])
      includePaths = eval(output)
      for idx, _path in enumerate(includePaths):
         sys.path.insert(idx, _path)
      print('use_debug='+os.environ['use_debug'])
      import ptvsd
      print("[ptvsd] enable_attach at localhost:3000")
      ptvsd.enable_attach(address=('localhost', 3000), redirect_output=True)
      print("[ptvsd] wait_for_attach")
      ptvsd.wait_for_attach()   
      print("[ptvsd] happy debugging!")
except Exception as e:
   import traceback
   track = traceback.format_exc()
   print(track)
   sys.exit(-1)

So here is what the above code does:

  • Check for use_debug env, if it exists and equal 'true' then we continue, if not then we just run RenPy like usual;
  • Collect sys.path from PythonS and insert it into RenPy's sys.path;
  • Import ptvsd (we can do this thanks to the previous sys.path collecting step) and use it to open a remote debugging server, using url: localhost:3000;
  • Wait for a debugger to attach;

Finally, if you want to debug your python code, you can run your project directly like this:

  • For CMD:
# replace the following paths with your corresponding paths
>> set use_debug=true
>> C:/renpy-7.3.2/lib/windows-i686/python -EO C:/renpy-7.3.2/renpy.py C:/PathToYourProjectDirectory
  • For bash:
# replace the following paths with your corresponding paths
>> use_debug=true C:/renpy-7.3.2/lib/windows-i686/python -EO C:/renpy-7.3.2/renpy.py C:/PathToYourProjectDirectory

Then just use your favourite Python debugger to attach to it. The only drawback is that this method cannot be used to debug *.rpy file directly, only *.py file is debuggable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment