os.getcwd() in sage

49 views
Skip to first unread message

Андрей Ширшов

unread,
Aug 2, 2014, 9:10:19 AM8/2/14
to sage-s...@googlegroups.com
Hi!
os.getcwd works very strange for me.
For example I have mylib folder. There are 2 files in it:
  1. mymodule.py
  2. mystyles.css

File mymodule.py has the following strings:

import sage_salvus as salvus # this in for sagemathcloud
import os
cur_path
= os.getcwd() + '/'
salvus
.load(cur_path + 'mystyles.css')

I added path to mylib folder in sys.path. When I try to import my module I get error that file mystyles.css not found. And I see that sage try to find this file in directory of current file (in which I import mymodule).

How can I fix my error?

Thanks, Andrei.

Андрей Ширшов

unread,
Aug 2, 2014, 11:27:45 AM8/2/14
to sage-s...@googlegroups.com
I found a solution :)

cur_path = os.path.realpath(__file__) + "/"
salvus
.load(cur_path + 'mystyles.css')

Thanks, Andrei.

суббота, 2 августа 2014 г., 17:10:19 UTC+4 пользователь Андрей Ширшов написал:

Андрей Ширшов

unread,
Aug 2, 2014, 11:40:19 AM8/2/14
to sage-s...@googlegroups.com
Sorry, I was wrong :(
The right way is the following:
cur_path = "/".join(os.path.realpath(__file__).split('/')[:-1]) + "/"
salvus
.load(cur_path + 'mystyles.css')



суббота, 2 августа 2014 г., 19:27:45 UTC+4 пользователь Андрей Ширшов написал:

Dominique Laurain

unread,
Aug 2, 2014, 12:29:53 PM8/2/14
to sage-s...@googlegroups.com
Not directly related to your question...but for others...how to modify path and how to load python file

Alternative answer to question given in the SAGE cloud wiki-FAQ https://github.com/sagemath/cloud/wiki/FAQ

Question: I would like that all of my worksheets know where to find a given module that I write or install.


1.In your project, create new Folder   (example: modules)
2.Add one python source file on that folder (example; DrawingConstants.py with lines "DrawingConstants module loading...", various python lines, and "...DrawingConstants module loaded.")
3.In worksheet cell, if you want to use constant or function from DrawingConstants.py file

import sys
sys.path.append('./modules')
# sys.path
import DrawingConstants
...
print "constant zz from  DrawingConstants = ",DrawingConstants.zz

You have to Restart worksheet after you make changes to DrawingConstants.py.

Dominique

Андрей Ширшов

unread,
Aug 2, 2014, 12:40:18 PM8/2/14
to sage-s...@googlegroups.com
Thanks a lot!) It's not my case but it's very useful info for me.
Andrei.

суббота, 2 августа 2014 г., 20:29:53 UTC+4 пользователь Dominique Laurain написал:
Reply all
Reply to author
Forward
0 new messages