[英]Python error with hello.py traceback
I'm very new to Python. 我是Python的新手。 I am having an issue with my hello.py command.
我的hello.py命令有问题。 It gives me the following error:
它给了我以下错误:
C:\Users\Admin>python hello.py
Traceback (most recent call last):
File "hello.py", line 1 in <module>
if _name_ == "_main_":
NameError: name '_name_' is not defined
Try using 2 underscores before and after name and main, so: 尝试在名称和主名称前后使用2个下划线,因此:
__name__
And 和
__main__
Try putting this in your hello.py
: 尝试将其放在您的
hello.py
:
def myfunction():
print "hello!"
if __name__ == "__main__":
myfunction():
Enclose the code you have in the hello.py
script in a function wrapper ( myfunction()
in above example). 将您在
hello.py
脚本中包含的代码封装在函数包装中(上例中的myfunction()
)。 Now, when executing hello.py
from the command line , the myfunction()
will be called by the if __name__ == "__main__":
part) 现在, 从命令行执行
hello.py
时, if __name__ == "__main__":
部分将调用myfunction()
)
If you want to import hello.py
as a Python module in another Python script, say anotherPython.py. 如果要在另一个Python脚本中将hello.py
import hello.py
为Python模块,请说另一个Python.py。 Place an empty file in the same directory as the hello.py
, whose name is exactly: __init__.py
. 将一个空文件放置在与
hello.py
相同的目录中,该hello.py
的名称恰好是__init__.py
。 Then in the anotherPython.py, write: 然后在anotherPython.py中编写:
import hello
hello.myfunction()
That should then print "hello!" 然后应该打印“你好!” when executed in Python.
在Python中执行时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.