我无法运行python文件与终端
问题描述:
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。
谢谢你,
答
您可以使用python -i script.py
这种方式,执行script.py然后蟒蛇进入交互模式。在交互模式下,您可以使用脚本中定义的所有函数,类和变量。
答
尝试脚本名称
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.
答
可以使用的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))
您在哪里调用您的myFunction? – manvi77
我想在终端内调用它 –