How to add a custom flag to IPython's magic commands? (.ipy files)


Is it possible to add a custom flag to IPython's magic command? To be more specific, I want to use the %run command with a homemade flag:

%run script.ipy --flag "option"

and be able to use "option" inside the script.

For .py files, the answer is provided here: <a href="https://stackoverflow.com/questions/1009860/command-line-arguments-in-python" rel="nofollow">Command Line Arguments In Python</a>


As indicated in the comments, this question isn't just about handling commandline arguments in a Python script. It's about handling them in an .ipy file run via %run.

If I create test.ipy as

import sys print(sys.argv)

and run it from shell, I see the commandline arguments:

1223:~/mypy$ python3 test.ipy -test foo ['test.ipy', '-test', 'foo']

but from a ipython session, I don't

In [464]: %run test.ipy --flag test ['/usr/bin/ipython3']

If I make a copy with a py name

In [468]: %run testipy.py --flag test ['testipy.py', '--flag', 'test']

So the %run ...ipy behaves differently. This is a ipython issue, not a general Python commandline one.


The %run doc has this point:


There is one special usage for which the text above doesn't apply: if the filename ends with .ipy[nb], the file is run as ipython script, just as if the commands were written on IPython prompt.


In that case, the test.ipy script is seeing the same sys.argv as I do when I type:

In [475]: sys.argv Out[475]: ['/usr/bin/ipython3']

So if I modify sys.argv in the current session, such as by appending a couple of strings:

In [476]: sys.argv += ['--flag','test'] In [477]: sys.argv Out[477]: ['/usr/bin/ipython3', '--flag', 'test'] In [479]: %run test.ipy ['/usr/bin/ipython3', '--flag', 'test']

my ipy script now sees them.

So that's the answer - put the commandline arguments in the sys.argv prior to using %run ...ipy.

(I've done this sort of fiddling with the sys.argv when doing advanced things with argparse.)

More ipython magic

In [480]: %run??

shows me both its doc and its code. I can thus see how it is treating a .ipy file special. Since it's so easy to find, I'll refrain from copying it here.

There's another solution - don't try to use the commandline style of coding with ipy files.

If I add a


line to that test file, and have an x defined in my Ipython session, I see that print. But I put that same print in a .py, I'll get a Nameerror. When they say the ipy is run as though it were typed in, they mean it. Running an ipy is, in effect, an alternative to %paste from the clipboard.


  • Mypy error - incompatible types in assignment
  • Custom session id generation implement UuidGenerator
  • mypy Error TypeVar with Value Restriction and Union of Unions / Optional Cannot Pass generic contain
  • mypy: Signature of “__getitem__” incompatible with supertype “Sequence”
  • Can't install scipy
  • Calling main function from another function in C
  • ETXTBSY and how to override it
  • A simple question about type coercion in C++
  • Python kludge to read UCS-2 (UTF-16?) as ASCII
  • Using QProcess.finished() in Python 3 and PyQt
  • iPython Notebook not printing Dataframe as table
  • Product and product template in Odoo 10
  • Writing an RDA to CSV in R
  • OPENCV : CUDA context initialization for different methods
  • Pass Arbitrary Sized 2 Dimension Array
  • How to install PyAutoGUI
  • import error with python-mysql-connector 1.16, django 1.6, and python 3.2.3
  • What is lua_len() alternative in Lua 5.1?
  • How to set a thread specific environment variable in Python?
  • Error while importing scikits.talkbox
  • Call a macro with parameters : Python win32com API
  • Python adding lots of things to PATH. How do I stop?
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • How can I get the full list of running processes on a Mac from a python app
  • Getting short path in python
  • How to retrieve information from antrun back to maven?
  • How to read piped content in C?
  • How can I extract results of aggregate queries in slick?
  • Spark fat jar to run multiple versions on YARN
  • Meteor helpers not available in Angular template
  • Debugging ASP.NET on a built-in web server suddenly stops
  • JSON with duplicate key names losing information when parsed
  • Build own AppleScript numerical error handling
  • Jquery - Jquery Wysiwyg return html as a string
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • java string with new operator and a literal