## 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.

Hello from Russia!

Can I quote a post in your blog with the link to you?

> Can I quote a post in your blog with the link to you?

Yes you can link to this blog post.

How do you add Sage to your path?

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.

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

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

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

Thnx a lot..This is very helpful.

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

I’d rather use Tkinter with Sage.

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