Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我能';t与终端交互运行python文件_Python_Macos_Terminal - Fatal编程技术网

我能';t与终端交互运行python文件

我能';t与终端交互运行python文件,python,macos,terminal,Python,Macos,Terminal,我正在使用OSX Mac终端运行python 2.7.10 例如: 我有一个名为“myfile.py”的文件 因此,当我想在终端上运行它时,它将如下所示: python Desktop/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))

我正在使用OSX Mac终端运行python 2.7.10

例如:

我有一个名为“myfile.py”的文件

因此,当我想在终端上运行它时,它将如下所示:

python Desktop/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))
但是在这个文件中,我写了一些函数,比如

def myFunction(x,y):
    return float(x)/y
使用这种运行python脚本的方法,我无法与我的程序交互,无法使用myFunction每次输入不同值的x和y,也无法正确测试myFunction


谢谢,

您可以使用
python-iscript.py


这样,执行script.py,然后python进入交互模式。在交互模式下,您可以使用脚本中定义的所有函数、类和变量。

尝试在脚本名称之前传递
-i

python -i myfile.py
通过运行
manpython
,您可以了解有关可用选项的更多信息

引用手册:

   -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代码可以如下所示:

python Desktop/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
?我想在终端内部调用它