parminder kaur
parminder kaur

Reputation: 15

How do I execute a function from another module?

I am using python 3.6 version. I have 2 .py files as:

modbody.py

def test():
    print("Statement goes here")

moduse.py

import modbody
test()

I am trying to execute moduse.py file in python interpreter using command "Python moduse.py" however, it gives me below error:

File "C:\users\Program\moduse.py", line 2, in test() NameError: name 'test' is not defined

Please help guide me how to execute the script and call the test function here? I have also tried adding a blank init.py file to solve this but with no luck. Please not all of my files are in same directory only.

Upvotes: 1

Views: 178

Answers (1)

U13-Forward
U13-Forward

Reputation: 71570

I think you meant:

import modbody
modbody.test()

Or:

from modbody import test
test()

Or:

from modbody import *
test()

Upvotes: 3

Related Questions