Abdullah .F
Abdullah .F

Reputation: 57

I can't run python file interactivly with terminal

I'm using OSX Mac terminal to run python 2.7.10.

for example:

I have a file called "myfile.py"

so when I want to run it on the terminal it would be like this:

python Desktop/myfile.py

However inside the file I have wrote some functions like

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

with this method of running a python script I can not interact with my program and use myFunction to input x and y with different values every time and test myFunction properly.

Thank You,

Upvotes: 1

Views: 530

Answers (3)

napuzba
napuzba

Reputation: 6288

You can use python -i script.py

This way, script.py is executed and then python enter interactive mode. In interactive mode you can use all functions, classes and variables that was defined in the script.

Upvotes: 1

eli-bd
eli-bd

Reputation: 1634

You can use raw_input() to do that.
Your myfile.py code can look like this:

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

Upvotes: 0

ハセン
ハセン

Reputation: 387

Try passing -i before the script name

python -i myfile.py

You can learn more about what options are available by running man python.

To quote from the manual:

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

Upvotes: 1

Related Questions