如何在终端上运行 Python 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21492214/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:58:46  来源:igfitidea点击:

How to run Python script on terminal?

pythonmacosterminal

提问by SnakeEyes

I want to run a Python script in Terminal, but I don't know how? I already have a saved file called gameover.py in the directory "/User/luca/Documents/python".

我想在终端中运行 Python 脚本,但我不知道如何运行?我已经在目录“/User/luca/Documents/python”中保存了一个名为 gameover.py 的文件。

回答by ferdynator

You need pythoninstalled on your system. Then you can run this in the terminal in the correct directory:

您需要在系统上安装python。然后你可以在终端的正确目录中运行它:

python gameover.py

回答by Enrique

First of all, you need to move to the location of the file you are trying to execute, so in a Terminal:

首先,您需要移动到您尝试执行的文件所在的位置,因此在终端中:

cd ~/Documents/python

Now, you should be able to execute your file:

现在,您应该能够执行您的文件:

python gameover.py

回答by bcho04

You can execute your file by using this:

您可以使用以下命令执行您的文件:

python /Users/luca/Documents/python/gameover.py

You can also run the file by moving to the path of the file you want to run and typing:

您还可以通过移动到要运行的文件的路径并键入以下内容来运行该文件:

python gameover.py

回答by error2007s

This Depends on what version of python is installed on you system. See below.

这取决于您的系统上安装的 python 版本。见下文。

If You have Python 2.* version you have to run this command

如果您有 Python 2.* 版本,则必须运行此命令

python gameover.py

But if you have Python 3.* version you have to run this command

但是如果你有 Python 3.* 版本,你必须运行这个命令

python3 gameover.py

Because for MAC with Python version 3.* you will get command not found error

因为对于带有 Python 版本 3.* 的 MAC,您将收到 command not found 错误

if you run "python gameover.py"

如果你运行“python gameover.py”

回答by Cro-Magnon

If you are working with Ubuntu, sometimes you need to run as sudo:

如果您正在使用 Ubuntu,有时您需要运行为sudo

For Python2:

对于 Python2:

sudo python gameover.py

For Python3:

对于 Python3:

sudo python3 gameover.py

回答by u9868018

You first must install python. Mac comes with python 2.7 installed to install Python 3 you can follow this tutorial: http://docs.python-guide.org/en/latest/starting/install3/osx/.

您首先必须安装python。Mac 安装了 python 2.7 来安装 Python 3,您可以按照本教程进行操作:http: //docs.python-guide.org/en/latest/starting/install3/osx/

To run the program you can then copy and paste in this code:

要运行该程序,您可以复制并粘贴以下代码:

python /Users/luca/Documents/python/gameover.py

Or you can go to the directory of the file with cdfollowed by the folder. When you are in the folder you can then python YourFile.py.

或者您可以转到文件的目录,cd然后是文件夹。当你在文件夹中时,你可以python YourFile.py

回答by Boris

Let's say your script is called my_script.pyand you have put it in your Downloads folder.

假设您的脚本已被调用,my_script.py并且您已将其放入“下载”文件夹中。

There are many ways of installing Python, but homebrewis the easiest.

安装 Python 的方法有很多,但自制软件是最简单的。

0) Open Terminal.app

0) 打开Terminal.app

1) Install homebrew (by pasting the following text into Terminal.app and pressing the Enter key)

1)安装自制软件(通过将以下文本粘贴到 Terminal.app 并按Enter 键

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2) Install Python using homebrew

2)使用自制软件安装Python

brew install python

3) cdinto the directory that contains your Python script (as an example I'm using the Downloads (Downloads) folder in your home (~) folder):

3)cd进入包含您的 Python 脚本的目录(例如,我正在使用Downloads您的 home ( ~) 文件夹中的 Downloads ( ) 文件夹):

cd ~/Downloads

4) Run the script using the python3executable

4) 使用python3可执行文件运行脚本

python3 my_script.py

You can also skip step 3 and give python3an absolute pathinstead

您也可以跳过步骤3,给python3一个绝对路径,而不是

python3 ~/Downloads/my_script.py


Instead of typing out that whole thing (~/Downloads/my_script.py), you can find the .pyfile in Finder.app and just drag it into the Terminal.app window which should type out the path for you.

~/Downloads/my_script.py您可以.py在 Finder.app 中找到该文件,而不是输入整个内容 ( ),然后将其拖到 Terminal.app 窗口中,该窗口应该为您输入路径。

If you have spaces or certain other symbols somewhere in your filename you need to enclose the file name in quotes:

如果文件名中有空格或某些其他符号,则需要将文件名括在引号中:

python3 "~/Downloads/some directory with spaces/and a filename with a | character.py"


Note that you need to install it as brew install pythonbut later use the command python3(with a 3at the end).

请注意,您需要将其安装为brew install python但稍后使用该命令python3(以 a3结尾)。