absk
absk

Reputation: 53

How do I run a function which is defined in a seperate .py file in Spyder

I have a function file from which I load my function. It is as follows:

def testFunc(A):
    B = get_rate('USD', 'JPY', datetime(2020,7,8).date()) * A
    return B

I also have a master file (in the same folder) from which I run everything.

from forex_python.converter import get_rate #necessary package

%run '/Users/mak/Dropbox/Python/Templates/getfx.py' #load function

testFunc(2) #run command

However, I get an error message as follows:

NameError: name 'get_rate' is not defined

If I run all the code in a single file, then it works:

from forex_python.converter import get_rate

def testFunc(A):
        B = get_rate('USD', 'JPY', datetime(2020,7,8).date()) * A
        return B

testFunc(2)

How do I solve my problem?

Note: The above is a simplification of my daily work flow. In reality I am dealing with much bigger files, which is why I can't simply put all the code in one file and run it.

Upvotes: 0

Views: 49

Answers (1)

Red
Red

Reputation: 27547

To solve the problem, import get_rate inside the file with the function.

Upvotes: 2

Related Questions