简体   繁体   English

带有hello.py追溯的Python错误

[英]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():

In other words 换一种说法

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()


Here's another way 这是另一种方式

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.

相关问题 AH01215:(8)执行格式错误:'/var/www/python/hello.py'的执行失败:/var/www/python/hello.py - AH01215: (8) Exec format error: exec of '/var/www/python/hello.py' failed: /var/www/python/hello.py hello.py: 错误: 需要以下 arguments: -i/--image - hello.py: error: the following arguments are required: -i/--image Flask hello.py 基本应用未运行? - Flask hello.py basic app not running? 运行 make -f hello.py 时出现错误。 它说“hello.py:1: *** 缺少分隔符。停止。” - I'm getting an error when running make -f hello.py. It says " hello.py:1: *** missing separator. Stop. " 在Android进程中运行hello.py - Running hello.py from within an Android Process Flask 错误'ImportError: cannot import name 'db' from 'hello' (C:\Users\admin\flask_stuff\venv\hello.py)' 当我尝试创建 SQL 数据库时 - Flask error 'ImportError: cannot import name 'db' from 'hello' (C:\Users\admin\flask_stuff\venv\hello.py)' when I try to create an SQL Database C++ "Hello World" 程序调用 hello.py 到 output 字符串? - C++ "Hello World" program that calls hello.py to output the string? 我无法运行 hello.py 并得到 SyntaxError: invalid syntax - I am unable to run hello.py and get SyntaxError: invalid syntax Windows Python 2.5 Traceback.py Nonetype错误 - Windows Python 2.5 Traceback.py Nonetype error discord\\client.py 错误中的python 脚本上的回溯错误? - Traceback errors on python script within the discord\client.py error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM