1

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,

asked Apr 21, 2017 at 6:27
2
  • where are you calling your myFunction? Commented Apr 21, 2017 at 6:29
  • I want to call it inside the terminal Commented Apr 21, 2017 at 6:33

3 Answers 3

1

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.
answered Apr 21, 2017 at 6:32
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Apr 21, 2017 at 6:30

Comments

0

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))
answered Apr 21, 2017 at 6:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.