Startup script with magics fails

Hello,
I have the following code in $HOME/.ipython/profile_default/startup/00-first.py:

%alias test echo "Test SUCCESS"
didrun=True

When I open a new notebook and check whether the startup script worked, neither the alias was set, nor the didrun variable is set:

In [1]: test

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-4e1243bd22c6> in <module>
----> 1 test

NameError: name 'test' is not defined

In [2]: didrun

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-b53c54e73e1c> in <module>
----> 1 didrun

NameError: name 'didrun' is not defined

When I remove the %alias command from the startup script, didrun is set as expected.

What do I need to change so that a startup script with magics succeeds? Or is there another configuration option to execute a bunch of commands whenever a new notebook is started?
The %alias is only one example magic command. The solution should apply for any other magic command.

Thank you
Ernst

Hi Ernst,
I believe the issue is that magics aren’t Python, and you designated that as a Python script with the extension .py.
According to the Startup Files documentation, you can add IPython scripts that end with .ipy.

I just tested renaming the file from ending in .py to .ipy via:

mv 00-first.py 00-first.ipy

And then the assigned alias worked and didrun was True when I started a new IPython session or I started a new Jupyter notebook.

2 Likes