Home > documentation, programming, Python, Sage, symbolic computation > How to use Sage as a Python library

How to use Sage as a Python library

Yes you can import Sage as a library in a Python script. One caveat is that you need to run that Python script using the version of Python that is bundled with Sage; currently Python 2.6.x. To import Sage, put the following in your Python script:

from sage.all import *

Then when you want to run your script, you need to invoke Sage with the option “-python” which would run your script using the Python that comes with Sage. For example, if Sage is in your PATH variable then you can do this:

sage -python /path/to/my/script.py

Another way is to write a Sage script and run that script using Sage itself. A Sage script has the file extension “.sage” and is more or less a Python script but uses Sage-specific functions and commands. You can then run that Sage script like so:

sage /path/to/my/script.sage

This will take care of loading the necessary environment variables and default imports for you.

Say you have a Python script called “my_script.py”. In a Sage session, you can load that script as follows:

sage: load "/path/to/my_script.py"

When writing code, use the command attach() within a Sage session to automatically reload the script containing the code every time you save your changes.

Advertisements
  1. 16 October 2009 at 3:48 pm

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

  2. 1 November 2009 at 6:22 am

    > Can I quote a post in your blog with the link to you?
    Yes you can link to this blog post.

  3. Jacob
    31 December 2010 at 3:23 am

    How do you add Sage to your path?

  4. Jacob
    31 December 2010 at 3:26 am

    Never mind, I just got it to work. I would be interested to hear what the “right” way to do it is though. I used sudo vi /etc/paths.

  5. mvngu
    2 January 2011 at 8:24 am

    Jacob :

    How do you add Sage to your path?

    Find out the absolute path to your local Sage installation and edit your $HOME/.bashrc accordingly. For example, say the absolute path to your local Sage installation is

    /path/to/SAGE_ROOT/sage
    

    Then add the following line to your $HOME/.bashrc:

    PATH=$PATH:/path/to/SAGE_ROOT/sage
    

    Save your edited $HOME/.bashrc, exit your terminal, and open a new terminal. Sage should now be in your path.

  6. 20 April 2011 at 4:53 am

    Thnx a lot..This is very helpful.
    Do you know a method to integrate sage+wxPython together..?

  7. mvngu
    20 April 2011 at 6:11 am

    RuwanWickramarachchi :
    Do you know a method to integrate sage+wxPython together..?

    You should direct support questions to the group sage-support at http://groups.google.com/group/sage-support

  8. Nal
    20 April 2011 at 7:31 pm

    I’d rather use Tkinter with Sage.

  9. Ashok
    1 October 2014 at 8:49 am

    Hi, I am using windows. I’ve downloaded sage but don’t know how to use it with Python. Any help?

  1. 31 January 2010 at 2:01 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: