Ipython | Unix commands not working in a script but working in a command line interpreter

Below the lines, when I put them in test.py and run it, it gives me an error, but it works fine when I run them from the 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 doesn't work?

+4
source share
1 answer

.pythe file is a python script, they must be pure python, IPython will not try magic on it. You must rename your script to .ipyif you want to use syntactic sugar. IPython suggestions on top of pure python syntax.

, IPython python (cf %hist vs %hist -t), python, IPython.

+3

Source: https://habr.com/ru/post/1527448/


All Articles