I'm trying to do something similar with: Load module from string in python
Sometimes I read source code provided by the user:
module = imp.new_module('AppModule')
exec_(s, module.__dict__)
sys.modules['AppModule'] = module
return module
And sometimes import from a file
module = __import__(module_name, fromlist=_fromlist)
return module
These modules are loaded to map objects and it'll be used inside a IDE.
My problem is: If there is any method call outside if __name__ == '__main__':
this code is being executed and can interact with the IDE.
How can I import modules ignoring methods from being executed?