4

Below lines when i put them in test.py and runs it, gives me error but runs fine when i run them from command line.

pyvar = 'Hello world'
!echo "A python variable: {pyvar}"

jitu@jitu-PC:~/ipython/python$ ipython test.py 
 File "/home/jitu/ipython/python/test.py", line 2
 !echo "A python variable: {pyvar}"
 ^
 SyntaxError: invalid syntax

Any idea why it is not working ?

3
  • 1
    Rename your script to .ipy Commented Feb 18, 2014 at 9:05
  • @Matt maybe your comment could be an answer, with more explanation? Commented Feb 18, 2014 at 15:45
  • Yes, done, was hard to write answer from my phone. Commented Feb 18, 2014 at 20:55

1 Answer 1

3

.py file are python script, they are supposed to be pure python, IPython will not try to do some "magic" on it. You should rename your script to .ipy if you want to use the syntactic sugar IPython offers on top of pure python syntax.

Note that all IPython syntactic sugar can be transformed into pure python (cf %hist vs %hist -t) that will be valid python syntax, but still need to have access to an IPython instance.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.