Ask Your Question

Writing re-usable sage scripts

asked 2011-03-17 11:00:10 +0200

NevilleS gravatar image

I'm looking to write some reusable code to do things like parse a particular file format outputted by some of my tools, but I don't see a particularly obvious "right way" to do this. I can define a python module, add it to the python path using the init.sage file, and then import it, but then I don't get some of the nice features I would get if I just loaded a .sage file from my current directory (namely tab-complete...).

Is there a preferred directory where I can save .sage files for use with the "load" commmand? If so, is there a way to specify a directory that is not in the SAGE_ROOT area, so I can source-control my specific .sage files independent of the sage installation?


edit retag flag offensive close merge delete



Not sure about your question, but I would use 'attach' instead of load, all things being equal. It makes it really easy for me to make quick changes to the code of my module and not have to reload.

kcrisman gravatar imagekcrisman ( 2011-03-17 11:35:17 +0200 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2011-03-17 14:39:30 +0200

Mike Witt gravatar image

This may not be the "right way" to do it, but here is what I do:

In my home directory, in the .sage subdirectory I have the following "init.sage" file:

CODE = '/home/mike/Projects/Sage/'
attach CODE+'defaults.sage'

Everything that I always want is in defaults.sage. Then in a particular sage program, I do something like:

attach CODE+'Differential_Equations/series.sage'
attach CODE+'Differential_Equations/pde_misc.sage'
attach /home/mike/Projects/Science/Electrodynamics/Electrostatics.sage

Note that while a couple of the things I needed were in my default "CODE" place, one of them was just in a random directory, so I had to include the whole path.

This certainly isn't perfect, but it's the best thing that I have found to do. I would also be interested in hearing other ideas. Figuring out the best was to "organize" my sage code has been an ongoing issue for me. I have never found a reasonable way to do it for sage notebooks.

edit flag offensive delete link more


Well, that's certainly better than what I had thus far. Thanks for this.

NevilleS gravatar imageNevilleS ( 2011-03-17 15:52:20 +0200 )edit

Since Sage 4.6.1, you can do `load_attach_path('/home/mike/Projects/Sage/')` and then `attach 'defaults.sage'` etc. should just work.

Felix Lawrence gravatar imageFelix Lawrence ( 2011-03-17 20:01:33 +0200 )edit

answered 2011-03-21 09:32:03 +0200

NevilleS gravatar image

I'm going to repost Felix's comment as my chosen answer, since it could be missed in the comments there:

Since Sage 4.6.1, you can do load_attach_path('/home/mike/Projects/Sage/') and then attach 'defaults.sage' etc. should just work.

This works reasonably well for me since I can add my "library" path to the sage path and define sage files there.

edit flag offensive delete link more

answered 2011-03-17 14:54:17 +0200

niles gravatar image

See also the question on organizing a multifile project for some ideas similar to those of @mike-witt. Unfortunately the conclusion there too was that there isn't really a good existing framework for things like this.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2011-03-17 11:00:10 +0200

Seen: 1,558 times

Last updated: Mar 21 '11