ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 11 Apr 2016 17:26:43 +0200get path of sage scripthttps://ask.sagemath.org/question/33053/get-path-of-sage-script/ Hi, I'm trying to cerate a sage script that will print it's location on the disk. the most of classical python solutions for this problem, like using the inspect module, doesn't work - they return me the location of temporary files (the files inside the directory which returned by the function tmp_dir() ) instead of the directory of the source file.
Thank you!Sun, 10 Apr 2016 22:04:28 +0200https://ask.sagemath.org/question/33053/get-path-of-sage-script/Answer by tmonteil for <p>Hi, I'm trying to cerate a sage script that will print it's location on the disk. the most of classical python solutions for this problem, like using the inspect module, doesn't work - they return me the location of temporary files (the files inside the directory which returned by the function tmp_dir() ) instead of the directory of the source file.</p>
<p>Thank you!</p>
https://ask.sagemath.org/question/33053/get-path-of-sage-script/?answer=33061#post-id-33061I am not sure about your question,so perhaps could you make explicit which script you want to run, from where, how it calls Sage, etc, so that we can make an adapted answer.
Anyway, note that from a Sage interpreter, you can get the directory that contains the `sage` command as follows (in my case, it is `/opt/sagemath/sage-source`):
sage: import os
sage: os.getenv('SAGE_ROOT')
'/opt/sagemath/sage-source'
Mon, 11 Apr 2016 15:36:34 +0200https://ask.sagemath.org/question/33053/get-path-of-sage-script/?answer=33061#post-id-33061Answer by John Palmieri for <p>Hi, I'm trying to cerate a sage script that will print it's location on the disk. the most of classical python solutions for this problem, like using the inspect module, doesn't work - they return me the location of temporary files (the files inside the directory which returned by the function tmp_dir() ) instead of the directory of the source file.</p>
<p>Thank you!</p>
https://ask.sagemath.org/question/33053/get-path-of-sage-script/?answer=33066#post-id-33066If you attach a file "mine.sage" containing a function `f` and you want to find the path of that file, you can do
from sage.misc.sageinspect import sage_getfile
sage_getfile(f)
This will return the location of the associated temporary file, the top of which should say
# -*- coding: utf-8 -*-
######################################################################
# This file was *autogenerated* from the file /path/to/mine.sage.
######################################################################
So read that file (using standard Python tools) and parse line 3 to get the path.
Mon, 11 Apr 2016 17:26:43 +0200https://ask.sagemath.org/question/33053/get-path-of-sage-script/?answer=33066#post-id-33066