Ask Your Question

How to set the working or current directory in the notebook?

asked 2016-08-20 06:53:59 +0200

Alasdair gravatar image

If I start sage in my directory ~/Sage/ then !pwd returns that directory. If I start a notebook though, the directory, given with

import os

is a temporary directory within the /tmp tree. And if I try to change that directory with os.chdir("/home/me/Sage") then that change doesn't seem to "stick", in that the second time I call os.getcwd() it gives me the /tmpdirectory again.

What I want to do is to run the Sage notebook in such a way that I can load files from its current working directory. I tried starting the notebook with notebook("/home/me/Sage") but it seems to make no difference. Unless of course the results of os are wrong.

How can I fix the working directory for use within the notebook?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-08-20 12:19:40 +0200

tmonteil gravatar image

updated 2016-08-20 12:20:27 +0200

The previous trick does not work anymore. As a workaround, you can just load your stuff with absolute path:


Another possibility is to use jupyter notebook (sage -n jupyter).

edit flag offensive delete link more

answered 2016-08-22 02:02:35 +0200

slelievre gravatar image

You should use the Jupyter notebook.

Launch it by running

$ sage -n jupyter

after cd-ing to the location you want.

This will open your browser at the page localhost:8888/tree

Then click "New" and in the pop-up menu select "SageMath 7.x".

Your browser will now be at http://localhost:8888/notebooks/Untitled.ipynb?kernel_name=sagemath.

Then type in some code in the input cells; get output cells when you evaluate them.

edit flag offensive delete link more


Note that the Sage notebook still has some advantages over jupyter, in particular, the live documentation, and the possibility to import worksheets as .rst files.

tmonteil gravatar imagetmonteil ( 2016-08-22 03:41:51 +0200 )edit

@tmonteil, you are right. Work on support for live documentation in Jupyter notebook worksheets is ongoing at Sage trac ticket #20690 Live documentation in Jupyter using Thebe with follow-up work on further functionality at Sage trac ticket #20893 Improve live documentation in the Jupyter notebook.

slelievre gravatar imageslelievre ( 2016-08-22 15:29:43 +0200 )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

1 follower


Asked: 2016-08-20 06:53:59 +0200

Seen: 10,960 times

Last updated: Aug 22 '16