## Question

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

## Background

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 close merge delete

Sort by » oldest newest most voted

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
os.chdir(os.path.dirname(__file__))


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

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.

( 2010-12-21 04:05:48 +0100 )edit

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.

more

2

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.

( 2010-12-20 08:38:51 +0100 )edit

Thanks for the correction, Felix!

( 2010-12-20 09:59:55 +0100 )edit