IPython | Unix commands not working in script but working in command line interpreter - ipython

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 ?

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

Related

ipython magic/macro/alias guidance for invoking shell and dispatching result

(Note: I have plenty of python and unix shell experience, but fairly new to ipython -- using 7.5)
I'm trying to replicate a UNIX shell function that I use all the time, so that it works in the ipython shell.
The requirement is that I want to type something like to myproj, and then have ipython process the resulting text by doing a cd to the directory that comes back from to. (This is a quick-directory-change utility I use in unix)
The way it works in unix is that a shell function invokes an external command, that command prints its result to stdout, and the shell function then invokes the internal cd to the target dir.
I've been trying to wrap my head around %magic and macros and aliases in ipython, but so far I don't see how to get this done. Any ideas?

sys.argv is different when in interactive mode in Ipython

When I run a script with Ipython in interactive mode, the sys.argv argument list is different in the interactive part of the execution than in the script.
Is this a bug, or am I doing something wrong?
Thanks!
oskar#RR06:~$ cat test.py
import sys
print(sys.argv)
temp = sys.argv
oskar#RR06:~$ ipython -i test.py -- foo bar
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
Type "copyright", "credits" or "license" for more information.
IPython 4.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
['/home/oskar/test.py', 'foo', 'bar']
In [1]: temp
Out[1]: ['/home/oskar/test.py', 'foo', 'bar']
In [2]: sys.argv
Out[2]: ['/usr/local/bin/ipython', '-i', 'test.py', '--', 'foo', 'bar']
If I just invoke ipython, and look at sys.argv I get
In [3]: sys.argv
Out[3]: ['/usr/bin/ipython3']
Your Out[2] looks the same - the full list as provided by the shell and Python interpreter. Remember we are running a Python session with ipython import:
#!/usr/bin/env python3
# This script was automatically generated by setup.py
if __name__ == '__main__':
from IPython import start_ipython
start_ipython()
/usr/bin/ipython3 (END)
But look at ipython -h; in the first paragraph:
it executes the file and exits, passing the
remaining arguments to the script, just as if you had specified the same
command with python. You may need to specify -- before args to be passed
to the script, to prevent IPython from attempting to parse them.
So it's explicitly saying that
ipython -i test.py -- foo bar
becomes (in effect) - or is run as:
python test.py foo bar
The ipython code has a parser (as subclass argparse) that handles many different arguments. But ones it can't handle, or follow -- are set aside, and put in the sys.argv that your test.py sees.
But apparently that sys.argv is not what is given to the interactive session.
I think you'd get the same effect
$ipython
In[0]: %run test.py foo bar
...
%run saves the current sys.argv, and constructs a new one with sys.argv = [filename] + args. Then after running your test.py it restores the sys.argv.
This is not a bug, and you aren't doing anything wrong - except expecting the two sys.argv to be the same. It appears that in a plain Python shell, the two sys.argv are the same (without any of the options that the shell itself uses).

Executing a python file from IPython

I'm relatively new to NumPy/SciPy and IPython.
To execute a python script in the python interactive mode, we may use the following commands.
>>> import os
>>> os.system('executable.py')
Then the print outputs can be seen from the python prompt.
But the same idea doesn't work with IPython notebook.
In [64]:
import os
os.system('executable.py')
Out[64]:
0
In this case, I cannot see any print outputs. The notebook only tells weather execution was successful or not. Are there any ways to see the outputs when I use IPython notebook?
Use the magic function %run:
%run executable.py
This properly redirects stdout to the browser and you will see the output from the program in the notebook.
It gives you both, the typical features of running from command line plus Python tracebacks if there is exception.
Parameters after the filename are passed as command-line arguments to
the program (put in sys.argv). Then, control returns to IPython's
prompt.
This is similar to running at a system prompt python file args,
but with the advantage of giving you IPython's tracebacks, and of
loading all variables into your interactive namespace for further use
(unless -p is used, see below).
The option -t times your script. With -d it runs in the debugger pdb. More nice options to explore.

How to run IPython script from the command line - syntax error with magic functions, %

I want to run IPython from the command line. However, I get a syntax error on the first line, importing pylab with the magic function %pylab is giving a syntax error on the %. The command I am using is simply ipython -i script.py.
Any ideas how to solve this?
You need to name your file script.ipy. When it ends in .ipy it can contain ipython syntax.
From ipython --help:
Usage
ipython [subcommand] [options] [files]
If invoked with no options, it executes all the files listed in sequence
and exits, use -i to enter interactive mode after running the files. Files
ending in .py will be treated as normal Python, but files ending in .ipy
can contain special IPython syntax (magic commands, shell expansions, etc.)

python.exe output

I've got an application that uses python as a script language. When somebody modifies a python script i want to known if it has syntax errors. I am using the python.exe (windows 2003, python 2.5) to check the script so i execute something like
python script.py > errors.txt
but errors.txt remains 0kb despite script.py has syntax errors, in that case, the errors detected by python are showed in the screen (but not redirected to the file)
If python uses the STDERR output stream for errors (I admit I do not know if it does), you must insted redirect the error strem by using:
python script.py 2> errors.txt