I'm trying to call a small helper-function defined in 'timex.py' which is called from 'caller.py', which in turn should get called from (PyCharm's) IPython-console through "%run caller.py".
This currently results in "NameError: name 'pickle' is not defined".
# timex.py
from IPython import get_ipython
def time_expression(exp: str):
print(f"time expression: {exp}", flush=True)
return get_ipython().magic(f"timeit {exp}")
# caller.py
import pickle
from timex import time_expression
packet = [*range(10**2)]
time_expression("pickle.dumps(packet, protocol=-1)")
# Then run from IPython-console:
>>>%run caller.py
time expression: pickle.dumps(packet, protocol=-1)
NameError: name 'pickle' is not defined
Obviously the namespace of the ipython timeit-magic doesn't contain caller.py-globals. Passing globals() as argument of time_expression and updating globals from within time_expression didn't help, as well as passing get_ipython() as argument did not do the trick.
Is there a way to make it work?
It works if I define time_expression in caller.py
and called from Terminal with "ipython caller.py", but I'd prefer to put it in a library and just import it into where needed.
(Python 3.6.3, IPython 6.2.1)