It looks like you're trying to build a custom Python shell. Like the normal interactive Python interpreter, but with a few predefined functions. The code module can do that for you.
Let's create a shell with a single predefined function:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import readline # not really required, but allows you to
# navigate with your arrow keys
import code
def predefined_function():
return "whoop!"
vars = globals().copy()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
(Code gratefully stolen from this answer.)
Now, let's run it, shall we?
$ python pyshell.py
Python 2.7.5 |Anaconda 1.8.0 (64-bit)| (default, Jul 1 2013, 12:37:52) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> predefined_function()
'whoop!'
>>> a = 1
>>> print (a + 1)
2