Ask Your Question

Can I alias a sage command?

asked 2015-08-31 13:42:56 -0500

rexroni gravatar image

updated 2015-08-31 14:19:07 -0500

If I type alias, I get a list of aliases, but they all look very "bashy", and not like sage commands. Can I set an alias for a sage command? Like alias 'meh'='load("meh.sage")'?

I have looked on this site, the "SAGE for newbies" document I found by Ted Kosan,, and the general web, and I haven't found any documentation on aliases in sage.

When I run that, I get the following output:

sage: alias 'meh'='load("meh.sage")'
ValueError                                Traceback (most recent call last)
<ipython-input-118-7d3257e61ed9> in <module>()
----> 1 get_ipython().magic(u'alias \'meh\'=\'load("meh.sage")\'')

/usr/lib/sagemath/local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2302         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2303         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2304         return self.run_line_magic(magic_name, magic_arg_s)
   2306     #-------------------------------------------------------------------------

/usr/lib/sagemath/local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2223                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2224             with self.builtin_trap:
-> 2225                 result = fn(*args,**kwargs)
   2226             return result

/usr/lib/sagemath/local/lib/python2.7/site-packages/IPython/core/magics/osm.pyc in alias(self, parameter_s)

/usr/lib/sagemath/local/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    195         if callable(arg):

/usr/lib/sagemath/local/lib/python2.7/site-packages/IPython/core/magics/osm.pyc in alias(self, parameter_s)
    117         # Now try to define a new one
    118         try:
--> 119             alias,cmd = par.split(None, 1)
    120         except TypeError:
    121             print(oinspect.getdoc(self.alias))

ValueError: need more than 1 value to unpack
edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2015-09-10 22:59:10 -0500

nbruin gravatar image

"alias" is an IPython feature. The sage command line is IPython with some customizations. See for documentation.

edit flag offensive delete link more


Ah, thank you. That was the second half of the answer I was looking for. When I have 15 rep someday I'll come back and upvote your comment.

rexroni gravatar imagerexroni ( 2015-09-13 10:07:34 -0500 )edit

answered 2015-08-31 14:51:28 -0500

eric_g gravatar image


The easiest way is to define a Python function:

sage: def meh():
....:     load("meh.sage")
sage: meh()  # executes load("meh.sage")

Best wishes,


edit flag offensive delete link more


Ok, I did know about that, but I figured there must be a way to use the aliases, since aliases exist. Any insight into the aliasing then, if it isn't useful for sage commands?

rexroni gravatar imagerexroni ( 2015-09-02 12:42:08 -0500 )edit

answered 2018-12-17 02:18:25 -0500

florin gravatar image

Sorry, I'm missing some syntax. I tried

def LP():
    alias 'LP'='MixedIntegerLinearProgram'

gets the error

File "<ipython-input-4-fc93b025772b>", line 2
    alias 'LP'='MixedIntegerLinearProgram'
SyntaxError: invalid syntax
edit flag offensive delete link more


As explained by @nbruin, alias is not a proper Python command, so you cannot use it while defining a Python function with def. In your case, some correct syntax similar to what you wrote would be

def LP():
    lp = 'MixedIntegerLinearProgram'
    load(lp + '.sage')
eric_g gravatar imageeric_g ( 2018-12-17 19:23:26 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-08-31 13:42:56 -0500

Seen: 408 times

Last updated: Dec 17 '18