How to run Python script on terminal?

前端 未结 7 639
醉话见心
醉话见心 2020-11-28 08:19

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\".

相关标签:
7条回答
  • 2020-11-28 08:50

    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
    
    0 讨论(0)
  • 2020-11-28 08:54

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

    python gameover.py
    
    0 讨论(0)
  • 2020-11-28 08:54

    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.

    0) Open Terminal.app

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

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

    2) Install Python using homebrew

    brew install python
    

    3) 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
    

    4) 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 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"
    

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

    0 讨论(0)
  • 2020-11-28 08:55

    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
    
    0 讨论(0)
  • 2020-11-28 09:00

    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"

    0 讨论(0)
  • 2020-11-28 09:03

    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
    
    0 讨论(0)
提交回复
热议问题