使用导入时函数未定义错误

Function not defined error while using import

我在 python 中创建了两个文件 第一个是 mym.py

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

下一个是 abc.py

import mym

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

我得到的错误消息是...两个文件都在同一个工作目录中并且没有找不到模块的错误...它给出函数未定义的错误。

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

函数定义有什么问题我无法追踪...

需要将abc.py改为:

from mym import *

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

否则您将无法访问这些功能。

你可以这样试试:

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