Ask Your Question
4

working directory for load/attach

asked 2010-12-18 10:33:10 +0100

Daniel Krenn gravatar image

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

2 Answers

Sort by ยป oldest newest most voted
4

answered 2010-12-18 13:45:47 +0100

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

edit flag offensive delete link more

Comments

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-21 04:05:48 +0100 )edit
3

answered 2010-12-19 22:16:19 +0100

Mitesh Patel gravatar image

updated 2010-12-20 09:59:14 +0100

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

Comments

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.

Felix Lawrence gravatar imageFelix Lawrence ( 2010-12-20 08:38:51 +0100 )edit

Thanks for the correction, Felix!

Mitesh Patel gravatar imageMitesh Patel ( 2010-12-20 09:59:55 +0100 )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

Stats

Asked: 2010-12-18 10:33:10 +0100

Seen: 4,026 times

Last updated: Dec 20 '10