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

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

575524 次浏览

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

python gameover.py

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

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

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

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

python gameover.py

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

python3 gameover.py

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

if you run "python gameover.py"

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

For Python2:

sudo python gameover.py

For Python3:

sudo python3 gameover.py

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/.

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 cd followed by the folder. When you are in the folder you can then python YourFile.py.

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

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

  1. Open Terminal.app (press ⌘+Space and type "Terminal" and press the Enter key)

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

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  3. Install Python using Homebrew

    brew install python
    
  4. cd into the directory that contains your Python script (as an example I'm using the Downloads (Downloads) folder in your home (~) folder):

    cd ~/Downloads
    
  5. Run the script using the python3 executable

    python3 my_script.py
    

You can also skip step 3 and give python3 an absolute path instead

python3 ~/Downloads/my_script.py

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

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"

Open the directory where you have saved your python program

cd desktop/home/file.....

type the command

python3 filename.py

For OS Monterrey

/usr/local/bin/python3

or o Open the search bar on your mac and enter python a window will open with the address of the directory copy the directory and paste it into the terminal enjoy