Ask Your Question
4

working directory for load/attach

asked 14 years ago

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)?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
4

answered 14 years ago

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.

Preview: (hide)
link

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 ( 14 years ago )
3

answered 14 years ago

Mitesh Patel gravatar image

updated 14 years ago

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.

Preview: (hide)
link

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 ( 14 years ago )

Thanks for the correction, Felix!

Mitesh Patel gravatar imageMitesh Patel ( 14 years ago )

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: 14 years ago

Seen: 4,077 times

Last updated: Dec 20 '10