Ask Your Question

working directory for load/attach

asked 2010-12-18 03:33:10 -0500

Daniel Krenn gravatar image


Is there a command to change the current working directory inside sage, so that the load (or attach) command uses that directory as base?


I have the following file-directory-situtation:

  • directory project-base
    • file blub.sage (contains some useful functions)
    • file run.sage (contains a load blub.sage command)

To start the project i call sage run.sage in the directory project-base. Everything works fine.

If I now want to run the project from any other directory with sage path/project-base/run.sage, I get the error message, that the file tools.sage is not found. The error message is kind of clear, because in run.sage the file is loaded without any directory specifications.

A similar problem occours, when you want to call run.sage from the browser-notebook-interface.

Is there any possibility to solve this problem (except using absolute paths everywhere (which is clearly not a satisfying solution) or changing the directory structure or using soft-links)?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2010-12-18 06:45:47 -0500

Mike Hansen gravatar image

You can add the following lines to the top of run.sage in order to change the current directory to path/project-base

import os

How it works is that when you run sage path/project-base/run.sage, it converts run.sage into a Python module Within that module __file__ is a special global variable which is automatically set to the absolute path of that module.

edit flag offensive delete link more


Thanks, it works. I now do the following: First I store the current working directory, then I change like written in the answer above, and then I restore the original working directory again.

Daniel Krenn gravatar imageDaniel Krenn ( 2010-12-20 21:05:48 -0500 )edit

answered 2010-12-19 15:16:19 -0500

Mitesh Patel gravatar image

updated 2010-12-20 02:59:14 -0500

Sage 4.6.1 will allow you to specify a load/attach search path, which may also help. With the merged changes (ticket #378), you'll be able to consult load_attach_path? and reset_load_attach_path? for usage and examples.

edit flag offensive delete link more



load_attach_path is exactly the function that is designed to do what Daniel's looking for. HOWEVER it's not yet in an official version of Sage - it first appeared in 4.6.1.alpha1. Unless you're willing to download the alpha (or manually apply the patch on trac ticket #378), you'll have wait for 4.6.1 and use Mike's solution in the meantime.

Felix Lawrence gravatar imageFelix Lawrence ( 2010-12-20 01:38:51 -0500 )edit

Thanks for the correction, Felix!

Mitesh Patel gravatar imageMitesh Patel ( 2010-12-20 02:59:55 -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


Asked: 2010-12-18 03:33:10 -0500

Seen: 2,239 times

Last updated: Dec 20 '10