简体   繁体   English

我无法运行 hello.py 并得到 SyntaxError: invalid syntax

[英]I am unable to run hello.py and get SyntaxError: invalid syntax

I am new to Python and programming as a whole and I apologize for asking what may seem to be a duplicate question.我是 Python 和整个编程的新手,我很抱歉问了一个看似重复的问题。 However, I have been unable to run my own code using the following format:但是,我一直无法使用以下格式运行我自己的代码:

C:\Users\Archangel>python hello.py

This is what hello.py contains:这是 hello.py 包含的内容:

# Define a function
def world():
    print("Hello, World!")

I get the following response:我收到以下回复:

C:\Users\Archangel>python hello.py
  File "hello.py", line 1
    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
             ^
SyntaxError: invalid syntax

I have made sure the python file is in the C:\\Users\\Archangel folder.我已经确保 python 文件在C:\\Users\\Archangel文件夹中。 I should mention that I have tried having and still have the file (and other files that will not run as well) in locations contained in Path such as C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32我应该提到,我已经尝试在 Path 中包含的位置(例如C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32 )中拥有并且仍然拥有该文件(以及其他无法运行的文件)

The following is what I get from print(sys.path)以下是我从 print(sys.path) 得到的

>>> import sys
>>> print(sys.path)
['', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32\\lib', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32', 'C:\\Users\\Archangel\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
>>>

My Google searches have been fruitless and hours of going through similar questions here on Stack Overflow have not helped.我的谷歌搜索没有结果,在 Stack Overflow 上解决类似问题的时间也没有帮助。 Can anyone help or point me to a question that has been answered?任何人都可以帮助或指出我已回答的问题吗?

UPDATE更新

My programs contained the following lines: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.我的程序包含以下几行: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. from the IDLE shell I used to create the files.从我用来创建文件的 IDLE shell。

Deleting these lines has solved the problem.删除这些行就解决了这个问题。 Thanks to all of you.感谢大家。 I think this question should be deleted since the issue is elementary and clearly an error on my part.我认为这个问题应该删除,因为这个问题很初级,而且显然是我的错误。

There is no error in function definition, even without function call it should just execute without any output.函数定义没有错误,即使没有函数调用,它也应该在没有任何输出的情况下执行。

The error appears because python clearly thinks that the first line in the file looks like this:出现这个错误是因为python明显认为文件的第一行是这样的:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32

You can open the file in an alternative editor (notepad++ for example) and make sure that the file doesn't contain this line.您可以在其他编辑器(例如 notepad++)中打开该文件,并确保该文件不包含此行。

After that to actually get the "Hello World" output you need to add function call:之后要实际获得“Hello World”输出,您需要添加函数调用:

def world():
    print("Hello, World!")

world()

I can see below works fine.我可以看到下面工作正常。

# Define a function
def world():
    print("Hello, World!")

if __name__ == "__main__":
    world()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM