Notes to Self

View the Project on GitHub ssarabando

Debugging Python 3.4 scripts that require command line arguments with IDLE from a VirtualEnv

21 May 2015

To my future self: the detour I mentioned in the last post is bigger than anticipated and I’m still in Python lands.

Today I had to debug a Python script in production.

The script was running in a virtualenv and it required command line arguments.

I also needed IDLE to follow up the source and locals.

Setting that up was harder then I thought so I took the time to save it here for future reference.

First, open a command prompt with administrative privileges and cd into your virtualenv. Now, and since IDLE requires Tcl and Tk, you’ll have to symlink the required folders into the virtualenv’s Lib folder. In my case, that’s Tcl/Tk 8.6:

mklink /d tcl8.6 C:\Python34\tcl\tcl8.6
mklink /d tk8.6 C:\Python34\tcl\tk8.6

Next, cd into your project’s folder and activate your virtualenv. Run the following command:

python -c "from idlelib.PyShell import main; main()"

Now that IDLE is open, let’s prepare the environment. In the Python console, write:

import sys
filename = 'yourfilename.py'
# replace arg1 and arg2 with your arguments (add/remove as needed)
sys.argv = [filename, arg1, arg2]
g = globals()
g['__file__'] = filename

Time to turn the debugger on and write the next command:

exec(compile(open(filename, 'rb').read(), filename, 'exec'), g)

At this stage, the debugger will simply stop at the 1st line. Select the Source checkbox and Step and IDLE will open the file for you. You can now start debugging your code.

This solution was heavily influenced by the following posts: