# Using sagemath on windows 10 : impossible to attach file

Hi! I've installed sagemath on windows 10 and I'm going crazy. The problem it seems to be that all the program is runing in some kind of temporal directory . I leave here some feedback from my console:

sage: load_attach_path()

['.']


If I change the path,

sage: load_attach_path('C:\Users\Javier\Desktop\current\height1')

['.', 'C:\\Users\\Javier\\Desktop\\current\\height1']


So we can confirm the new path was added. Inside the folder height1 I have a file "'heigth1.sage". Let's try to attach it:

sage: attach("height1.sage")
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-4-cb4dd36405f2> in <module>()

252             # See Trac 11812.
253             if attach:
255             with open(preparse_file_named(fpath)) as f:
256                 code = compile(f.read(), preparse_file_named(fpath), 'exec')

376     sage.repl.inputhook.install()
377     fpath = os.path.abspath(filename)
--> 378     attached[fpath] = os.path.getmtime(fpath)
379
380

/opt/sagemath-8.2/local/lib/python2.7/genericpath.py in getmtime(filename)
60 def getmtime(filename):
61     """Return the last modification time of a file, reported by os.stat()."""
---> 62     return os.stat(filename).st_mtime
63
64

OSError: [Errno 2] No such file or directory: '/home/Javier/C:\\Users\\Javier\\Desktop\\current\\height1/height1.sage'


Recall the Error No such file or directory: '/home/Javier/C:\Users\Javier\Desktop\current\height1/height1.sage'. What ?? Please, help me ! I will appreciate your feedback very much!

edit retag close merge delete

Did you try absolute paths and write \ instead of /?

sage: attach('/home/Javier/C:\Users\Javier\Desktop\current\height1/height1.sage')
sage: attach('/home/Javier/C:\Users\Javier\Desktop\current\height1\height1.sage')

( 2018-07-09 16:45:27 -0500 )edit

I decided to move back to linux, so my comment doesnt have sense anymore. Thanks for the feedback!

( 2019-03-04 09:15:25 -0500 )edit

still, your question makes sense for other windows users

( 2019-03-22 14:39:08 -0500 )edit

Sort by » oldest newest most voted

Sage on Windows is still running in a UNIX-like environment called Cygwin. In Cygwin, you'll find that Windows style paths generally do work in Python. For example you'll find that the following works:

sage: print(open('C:\\Users\\Javier\\Desktop\\current\\height1\\height1.sage').read())


(Note the double-backslashes--the \ in Python is an escape character, so when you use Windows paths in Python you have to escape the backslash itself. Frequently Python will be forgiving of this if you have some \<C> that is not a known escape sequence. But if you have something like \n then it will still treat that as a newline (for example, since \n is a known escape sequence in Python).

However, it looks like there's a bug in os.path.abspath on Cygwin, where it does not recognize Windows-style paths as absolute paths. I'm glad you brought this up, since I believe that should actually be fixed in Python. This is because Python on Cygwin doesn't actually inherently understand Windows paths--the translation to UNIX-style paths happens transparently in the Cygwin layer. Oops!

You're better off using UNIX-style paths with forward-slashes. Check out what

sage: os.path.abspath('Desktop/current/height1/height1.sage')


returns. I think you'll be pleasantly surprised.

more

1

Note that prepending the string with "r" (for "raw") avoids the need to escape backslashes:

print(open(r'C:\Users\Javier\Desktop\current\height1\height1.sage').read())

( 2018-07-10 11:27:34 -0500 )edit

I decided to move back to linux, so my comment doesnt have sense anymore. Thanks for the feedback!

( 2019-03-04 09:15:38 -0500 )edit