I am trying to do something fairly simple. I want to import a function from a .py file but use modules imported in my primary script. For instance the following function is stored
./files/sinner.py
def sinner(x):
y = mt.sin(x)
return y
I then want to run sinner using the math as mt
from sinner import sinner
import math as mt
sinner(10)
This not surprisingly throws an error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-50006877ce3e> in <module>()
2 import math as mt
3
----> 4 sinner(10)
/media/ssd/Lodging_Classifier/sinner.py in sinner(x)
1 import math as mt
----> 2
3 def sinner(x):
4 y = mt.sin(x)
5 return y
NameError: global name 'mt' is not defined
I know how to handle this in R, but not in Python. What am I missing?
import mathinsinner.py. Every module is its own namespace. When the main module importssinner.py, it gets access tosinner.py's namespace: not the other way around.