如何在 Mac 上的终端中运行 python?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30408127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run python in terminal on a mac?
提问by Jay
I'm extremely new to programming, in fact I'm trying to learn Python now as a first language. I found that Google has a python course and it said to download python 2 from python's website, which I did, and I now realize that python was already installed on my mac by apple. Anyways, I also had to download some exercises from google, and attempt to run some code in terminal and this is where I'm running into trouble and could really use some insight.
我对编程非常陌生,实际上我现在正在尝试将 Python 作为第一语言学习。我发现谷歌有一个 python 课程,它说从 python 的网站下载 python 2,我做到了,现在我意识到苹果已经在我的 mac 上安装了 python。无论如何,我还必须从谷歌下载一些练习,并尝试在终端中运行一些代码,这就是我遇到麻烦并且可以真正使用一些见解的地方。
First, the lesson said to simply type, python
, into terminal to start coding in python (I don't know if this is the way to say that, or if that just gives you the ability to run python code?)
首先,课程中说只需python
在终端中键入 ,即可开始在 python 中编码(我不知道这是不是这样说,或者这是否只是让您能够运行 python 代码?)
Then, it said to type in: python hello.py
然后,它说输入: python hello.py
Which was supposed to return: Hello World
应该返回: Hello World
However, all I keep getting is :
但是,我不断得到的是:
SyntaxError: Invalid Syntax
语法错误:无效的语法
And I don't really know where to go from here, I was thinking maybe it's due to me downloading python again when it was already installed, if it even was?
而且我真的不知道从哪里开始,我在想也许是因为我在已经安装了 python 的情况下再次下载了它,如果它甚至是?
采纳答案by digawp
First, the lesson said to simply type, python, into terminal to start coding in python (I don't know if this is the way to say that, or if that just gives you the ability to run python code?)
首先,课程中说只需在终端中键入 python 即可开始在 python 中编码(我不知道是不是这样说,或者这是否只是让您能够运行 python 代码?)
What happens when you type a program name into terminal is that it basically executes the program, and if it has a GUI, the GUI window will pop up onto the screen. If it only has a command-line interface (like python
) the program will be executed inside the terminal.
当你在终端中输入程序名称时,它基本上会执行程序,如果它有 GUI,GUI 窗口会弹出到屏幕上。如果它只有命令行界面(如python
),则程序将在终端内执行。
In the case of running python
, what it does is actually to load a python interpreter inside your terminal. If you notice, the command-line interface would have changed quite a bit (I am on Windows so I am not sure how it looks like exactly on Mac). So instead of something like
在 running 的情况下python
,它实际上是在终端中加载一个 python 解释器。如果您注意到,命令行界面会发生很大变化(我在 Windows 上,所以我不确定它在 Mac 上的样子)。所以而不是像
YourName@USER ~/Path/to/your/exercise/files
$ _
at the start of the line, you would have encountered the below instead:
在该行的开头,您会遇到以下情况:
# Bunch of text describing your python version and other things
>>> _
That is actually the python interpreter's command line. You can only execute python codes here.
那实际上是python解释器的命令行。这里只能执行python代码。
Then, it said to type in:
python hello.py
Which was supposed to return:
Hello World
However, all I keep getting is :
SyntaxError: Invalid Syntax
然后,它说输入:
python hello.py
应该返回:
Hello World
但是,我不断得到的是:
SyntaxError: Invalid Syntax
I suspect when you are running the command python hello.py
, you were still inside the python interpreter program, which is why it return the InvalidSyntax
error, because it is not a valid python syntax.
我怀疑当您运行命令时python hello.py
,您仍在 python 解释器程序中,这就是它返回InvalidSyntax
错误的原因,因为它不是有效的 python 语法。
What python hello.py
does (if you were to run it in your terminal instead) is to execute the python interpreter, supplying your hello.py
file as the code for the interpreter to interpret and execute. So it is as if you run python
and entering the codes you wrote inside hello.py
into the python interpreter. So,
什么python hello.py
(如果您要在终端中运行它)是执行 python 解释器,提供您的hello.py
文件作为解释器解释和执行的代码。所以就好像你运行python
并输入你在里面写的代码hello.py
到 python 解释器中一样。所以,
I was thinking maybe it's due to me downloading python again when it was already installed, if it even was?
我在想也许是因为我在已经安装了 python 的情况下再次下载了它,如果它甚至是?
nope, it was not. It was because of the reasons I explained above.
不,它不是。这是因为我上面解释的原因。
Hope it helps!
希望能帮助到你!
Edit
编辑
A little bit of extra info, to properly exit the python interpreter command line, you can type exit()
or quit()
. Ctrl+D works too, as a shortcut.
一些额外的信息,要正确退出 python 解释器命令行,您可以输入exit()
或quit()
。Ctrl+D 也可以用作快捷方式。
回答by farhawa
python
command will open a Python interpreter, where you can't run the python hello.py
command so you got the syntax error (to run hello.py
in python shell you need execfile(hello.py)
) to quit the Python interpreter, type ctrl+d
and than python hello.py
to execute your hello world script.
python
command 将打开一个 Python 解释器,您无法在其中运行该python hello.py
命令,因此您会遇到语法错误(要hello.py
在您需要的 Python shell 中运行execfile(hello.py)
)以退出 Python 解释器,键入ctrl+d
并python hello.py
执行您的 hello world 脚本。
回答by pythongenius..not
type python into terminal. It will display what version of python you have installed then you can begin by typing:
在终端中输入 python。它将显示您安装的 python 版本,然后您可以输入:
print("hello world!")
python = completed it mate
python = 完成它 伙计