Finding missing modules and making them available

On a few occasions, I have needed to debug python code in some production application that I couldn’t easy get installed on my local box, either because it was old, or had a very custom setup, or I just didn’t have time and the problem needed to be fixed. I would copy over the python file I needed to mess with into my home folder, but it would almost definitely need to import modules that wouldn’t be found when I ran the script, as they too were in the original location. It wasn’t always clear exactly where, however.

I could run a recursive find command for the module(s) in a directory above where I grabbed the troubled file, but if there is a really large tree of directories in the application, that might take a while. Here’s an alternative. Open a python prompt in the directory where the problematic file was originally, try to import what it needs to import, and then run this for each imported module:

1
module.__file__

An example:

1
2
3
>>> import os
>>> os.__file__
'/usr/lib/python2.5/os.pyc'

Now you know exactly what you need to copy (note: grab the .py, not the .pyc, of course). Or if you don’t want to copy all the files, you can just add this to the file you are editing for debugging:

1
2
import sys
sys.path.append('/my/other/place')

And then the originals can be imported as desired. Except for really large projects, one or two appended paths will probably contain all you need.

Post to Twitter Post to Delicious Post to Digg Post to Reddit

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>