2017-04-21 55 views
1

interactivly我正在使用OSX Mac終端運行python 2.7.10。我無法運行python文件與終端

例如:

我稱之爲「myfile.py」

這樣一個文件時,我想在終端上運行它,它會是這樣:

python Desktop/myfile.py 

然而該文件我寫了一些功能,如

def myFunction(x,y): 
    return float(x)/y 

用這種方法運行python scr ipt我無法與我的程序進行交互,並使用myFunction每次輸入不同的值,並正確測試myFunction。

謝謝你,

+0

您在哪裏調用您的myFunction? – manvi77

+0

我想在終端內調用它 –

回答

1

您可以使用python -i script.py

這種方式,執行script.py然後蟒蛇進入交互模式。在交互模式下,您可以使用腳本中定義的所有函數,類和變量。

1

嘗試腳本名稱

python -i myfile.py 

您可以瞭解哪些選項可通過運行man python之前通過-i

從手動引用:

-i  When a script is passed as first argument or the -c option 
      is used, enter interactive mode after executing the script 
      or the command. It does not read the $PYTHONSTARTUP file. 
      This can be useful to inspect global variables or a stack 
      trace when a script raises an exception. 
0

可以使用的raw_input()來做到這一點。
您的myfile.py代碼可能如下所示:

def myFunction(x,y): 
    return float(x)/y 

x = raw_input("Please enter x value: ") 
y = raw_input("Please enter y value: ") 
print(myFunction(x,y))