Q:

Python if_py and VIM: exec('import re') does not work

I have a auto-completion script that I would like to modify
to complete class names, attributes, methods, etc. In python
when i do: re.co<TAB> it should give me a list of matching 
methods. Problem is, I don't want to parse the re.py file.
I'd prefer to:
import re

and then do dir(re) to get the list of methods. But How???
I tried:
imp_obj = exec('import re')

and it refused to work in if_py! 2 + 2 works though..

A:

The best way to do this is to use the builtin __import__ function to import the module like so:
imp_obj = __import__('re')

Your code probably does not work because import does not return a value that would in turn be returned by exec('import re').
In general, it is a bad idea to use exec on text input by the user because it has a higher probability of executing arbitrary code you don't want to execute.

