Debugging from pycharm (Ubuntu, macos)


#1

I thought that it might be a good idea to run the game from pycharm. I didn’t have much time to look into everything, but I see two problems:

  • How would the run configuration look like if I can start the game with the currently developed bot? I assume that all necessary files are in the project, imports work, etc.

  • Is it possible at all if halite is a compiled executable, and locally you can run it only as a shell script?


#2

I’m running PyCharm Pro on macOS, and was able to leverage remote debugging and a modified run_game.sh script:

Create a new “debug challenger” bot (I just copied MyBot.py to DebugBot.py) so that you have an opponent bot that won’t be trying to attach to the remote debugger.

Create a Python Remote Debug configuration per the docs at:
https://www.jetbrains.com/help/pycharm/run-debug-configuration-python-remote-debug.html
https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#remote-debug-config

Create a new debug_game.sh script:

#!/bin/sh
./halite --no-timeout --replay-directory replays/ -vvv --width 32 --height 32 "python3 MyBot.py" "python3 DebugBot.py"

The only changes from run_game.sh are disabling the timeout and changing one of the bots to DebugBot.py.

Sprinkle breakpoints in MyBot.py and start your remote debug session from PyCharm. Once it is listening for remote processes, execute ./debug_game.sh to launch the game. You should see the debugger in PyCharm stop at your breakpoint.

Good luck!


#3

Thanks for this awesome guide, it saved me a lot of time:)

I’d like to add that one should probably specify a different port than 0 on the localhost to connect to the debugger, since 0 can be occupied by something else.


#4

Thanks for this.

One thing that might be helpful for others. I noticed running run_game.sh required me to comment out the remote debug statements. Not a big deal, but I know I’ll forget that once or twice before submitting. Instead, I created a DebugMyBot.py wrapper which calls MyBot.py See the debug section of https://github.com/rpsoko/halite_iii/blob/a6bf709385d96b8f849468047639c3ffdc64194f/README.md for more details.


#5

One thing I actually wound up doing was adding an optional command line argument to enable the remote debugging connection, and defaulting it to be disabled. Then I modified my debug script to launch two copies of my bot, but one with the command line arg enabling debugging.

-R


#6

thanks for the guide!
alas i’m having problems connecting:

Blockquote
G:\Dropbox\Python\HaliteV1>halite.exe --no-timeout --replay-directory replays/ -vvv --width 32 --height 32 “python DebugMyBot.py” “python OldBot.py”
[info] Map seed is 1542134217
[info] [P0] Launching with command python DebugMyBot.py
[info] [P1] Launching with command python OldBot.py
[info] [P0] Initializing player
[info] [P1] Initializing player
[info] [P0] Initialized player Could not connect to localhost
[info] [P1] Initialized player MyPythonBot
[info] Player initialization complete
[error] [1] [P0] Communication failed
[error] [1] [P0] Last input received was:
[error] [1] [P0] Traceback (most recent call last):
[error] [1] [P0] File “C:\Users\Jan\AppData\Local\Programs\Python\Python37\lib\site-packages_pydevd_bundle\pydevd_comm.py”, line 549, in start_client
[error] [1] [P0] s.connect((host, port))
[error] [1] [P0] ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
[error] [1] [P0] Traceback (most recent call last):
[error] [1] [P0] File “DebugMyBot.py”, line 3, in
[error] [1] [P0] pydevd.settrace(‘localhost’, port=2222, stdoutToServer=True, stderrToServer=True)
[error] [1] [P0] File “C:\Users\Jan\AppData\Local\Programs\Python\Python37\lib\site-packages\pydevd.py”, line 1314, in settrace
[error] [1] [P0] stop_at_frame,
[error] [1] [P0] File “C:\Users\Jan\AppData\Local\Programs\Python\Python37\lib\site-packages\pydevd.py”, line 1363, in _locked_settrace
[error] [1] [P0] debugger.connect(host, port) # Note: connect can raise error.
[error] [1] [P0] File “C:\Users\Jan\AppData\Local\Programs\Python\Python37\lib\site-packages\pydevd.py”, line 356, in connect
[error] [1] [P0] s = start_client(host, port)
[error] [1] [P0] File “C:\Users\Jan\AppData\Local\Programs\Python\Python37\lib\site-packages_pydevd_bundle\pydevd_comm.py”, line 549, in start_client
[error] [1] [P0] s.connect((host, port))
[error] [1] [P0] ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
[warn] [1] [P0] Killing player

any idea?


#7

Change port to 5000+ maybe? And ensure that remote debugger is configured to the same port.


#8

same result :confused:
it connected when i set it to the debugger port here:

but when i hit the debugger button in pycharm it says


#9

Hmm, maybe you are using something wrong?

sys.path.append("pycharm-debug-py3k.egg")
import pydevd

pydevd.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True)

#10

what is this for? i followed @rpsoko 's guide:
https://github.com/rpsoko/halite_iii/blob/a6bf709385d96b8f849468047639c3ffdc64194f/README.md

still same problem


#11

You change something in Settings -> Debug, while you need to create Remote Debuger in Configurations menu. https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html (Remote debugging with the Python remote debug server configuration)
Edit: SO link https://stackoverflow.com/questions/35744587/how-to-remote-debug-in-pycharm


#12

i did all that of course. this is all i changed:

is there nothing else you guys did? do i have to change windows settings maybe?


Live (turn by turn) bot debubbing and developement in Python (Windows)