The docs for execfile() warn about wanting to modify a function local variables: it's not possible!
The default locals act as described for function locals() below: modifications to the default locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the code on locals after function execfile() returns. execfile() cannot be used reliably to modify a function’s locals.
It's not about execfile() but about locals():
def f():
locals()['a'] = 3
print(a)
You will also get NameError: global name 'a' is not defined
. This is possibly for optimization purposes. The solution here is to use a dictionary:
file_path = os.path.join('..', 'vessel_name.txt')
def my_input_func(file_path):
vessel = {}
execfile(file_path, vessel)
data = np.array([[vessel['hull_length']],
[vessel['hull_width'],
[vessel['etc.']])
return(data)
Note: I 'm assuming that you're using Python 2 but it would be the same in Python 3, except that execfile() is now exec() and you need to open the file yourself.