0

I want to know how to execute a python script using the command line. This is my code in Arithmetic.py:

def main(): 
 print 'spam' 
if __name__ == '__main__': 
 main() 

When I type

python Arithmetic.py

I get an Syntax Error on the "c"

File "ipython-input-11-0770a0dfdadd", line 1
 python Arithmetic.py

Any ideas? Thanks!

Carter Sande
1,87915 silver badges27 bronze badges
asked Mar 27, 2014 at 3:29
5
  • Is this ALL your code? Commented Mar 27, 2014 at 3:29
  • 4
    There is no "c" in the code you pasted. Are you typing "python Arithmetic.py" into the Python shell? Commented Mar 27, 2014 at 3:30
  • Sorry no ideas, as I don't know the exact error message and the complete code Commented Mar 27, 2014 at 3:31
  • @BrenBarn might be right, you can't call python Arithmetic.py from the python shell. You use that call from cmd or sh Commented Mar 27, 2014 at 3:32
  • @BrenBarn: yep, that's exactly how the syntax error would look if the OP typed it into the IPython shell. Commented Mar 27, 2014 at 3:32

2 Answers 2

3

It looks like you're trying to type the python Arithmetic.py command into an IPython shell. Instead, make sure you're using your operating system's command prompt. (You can get out of a Python or IPython shell by typing exit().)

On Windows, the command prompt looks like this:

C:\Users\Carter>

On Linux, the command prompt looks like this:

carter@carters-computer:~$

In Mac OS X, it looks like this:

Carters-Macbook:~ csande$
answered Mar 27, 2014 at 3:36
Sign up to request clarification or add additional context in comments.

Comments

2

To invoke your program from within ipython, use import Arithmetic (no '.py') in the same directory, followed by Arithmetic.main(). You can use the phrase you're currently trying to run your program from a command shell, without first starting an interpreter.

answered Mar 27, 2014 at 3:36

1 Comment

Note you can also use ipython's %run to run a file from within ipython.

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.