简体   繁体   English

从文件调用属性时的语法错误

[英]Syntax error when calling attributes from a file

I am trying to execute a file ( threenames.py ) with three attributes a, b, and c each assigned to a string as follows 我正在尝试执行一个具有三个属性a,b和c的文件( threenames.py ),每个属性分配给一个字符串,如下所示

a = 'New York'
b = 'Baltimore'
c = 'Phoenix'
Print (a, b, c)

and would like to call these using the python code as follows 并希望使用python代码如下调用它们

python threenames.py however I get a syntax error as follows
>>> python threenames.py
File "<stdin>", line 1
python threenames.py
                ^
SyntaxError: invalid syntax

What am I doing wrong? 我究竟做错了什么?

The >>> is the Python interactive interpreter's prompt. >>>是Python交互式解释器的提示。 python threenames.py is not a valid Python statement which is why it is giving you an error. python threenames.py不是有效的Python语句,这就是为什么它给您一个错误。

You can exit the Python REPL loop by hitting Ctrl + D or by executing exit() . 您可以通过Ctrl + D或执行exit()退出Python REPL循环。 You will then see you normal shell prompt (probably ending with $ ) and can execute python threenames.py . 然后,您将看到正常的shell提示符(可能以$结尾),并且可以执行python threenames.py

You can also instead just import threenames within the Python shell as it will execute all the code in that file but beware: if you update the code in your threenames.py file simply importing it again won't reload the code. 您也可以改为在Python Shell中import threenames ,因为它将执行该文件中的所有代码,但要注意:如果更新threenames.py文件中的代码,只需再次导入它就不会重新加载代码。


I'm assuming that your print formatting comes from actually using Python 2 instead of 3 as your syntax suggests; 我假设您的打印格式来自实际使用Python 2而不是您的语法建议的3。 use it as a statement instead and get rid of the tuple as so: 而是使用它作为语句,并这样摆脱元组:

a = 'New York'
b = 'Baltimore'
c = 'Phoenix'
print a, b, c

When you see the Python interactive interpreter's >>> prompt, type exit() and press Enter . 当您看到Python交互式解释器的>>>提示符时,键入exit()并按Enter After that you get your shell prompt back, where you can type python threenames.py and then press Enter to run the program. 之后,您会返回外壳程序提示,您可以在其中键入python threenames.py ,然后按Enter以运行该程序。

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

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