1

I created two files in python First one is mym.py

def hello():
 print("Hello everyone")
 return
def summ(x,y):
 total=x+y
 return total

and next one is abc.py

import mym
hello()
x=summ(3,4)
print(x)

And the error msg which I am getting is...both the files are in same working directory and there is no error of module not found...its giving error of function not defined.

Traceback (most recent call last):
 File "C:/Users/Nisha/AppData/Local/Programs/Python/Python39/abc.py", line 3, in <module>
 hello()
NameError: name 'hello' is not defined
Traceback (most recent call last):
 File "C:/Users/Nisha/AppData/Local/Programs/Python/Python39/abc.py", line 3, in <module>
 x=summ(3,4)
NameError: name 'summ' is not defined

What is the problem in function definition I am unable to trace...

U13-Forward
71.8k15 gold badges100 silver badges125 bronze badges
asked Dec 19, 2020 at 12:26
1
  • In this case, you need to prefix the name of the module on the function, so using mym.summ Commented Dec 19, 2020 at 12:31

2 Answers 2

1

The abc.py needs to be changed to:

from mym import *
hello()
x=summ(3,4)
print(x)

Otherwise you cannot access the functions.

answered Dec 19, 2020 at 12:26
Sign up to request clarification or add additional context in comments.

Comments

0

You can try like this:

 import mym
 mym.hello()
 x = mym.summ(3,4)
 print(x)
answered Dec 19, 2020 at 13:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.