ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 25 Jun 2019 12:27:38 -0500Creating a module in Sagehttp://ask.sagemath.org/question/46994/creating-a-module-in-sage/I am new to Sage, being used to Python and I am having some trouble with adapting.
In particular, I am trying to create a 'module' in the Python sense, i.e. some set of classes that I can call with 'import'.
When I did this in python in the past, I would create a directory (say /my_module/ in my home) and include a file __'__init____.sage' in that file with a line
__all__ = ['Submodule1', 'Submodule2']
Then I would have a file 'Submodule1' and 'Submodule2'.
In the file 'Submodule 1' I would define 'Class1'
and then from a file 'run.py' in home I would write
import my_module
from my_module.Submodule1 import Class1
and then I would be able to write
c=Class1()
to create an object.
I find that this is not working in Sage and the only thing I seem to be able to do is write in run.py
load("my_module/Submodule1.sage")
for each class I want to load. This has many disadvantages, for instance hiding classes from potential users so that they only exist internally.
Any suggestions on how to create and import modules in sage? What is the sage way?
Jesus Martinez GarciaTue, 25 Jun 2019 12:27:38 -0500http://ask.sagemath.org/question/46994/Can I create a sage file, and import it as a Python module?http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/ I am trying to create a file that produces some data using sage specific modules (eg graphs. ).
Is there a way that I can save this file and then import it in a Python shell and / or in a Sage shell?
The problems I have encountered so far are:
- if I try to save the file as filename.py and import it as a module in a Python or Sage shell, Python/ Sage won't recognize the sage specific functions (which makes sense)
- if I try to save the file as filename.sage and import it as a module in a Sage shell, I get the error "No module named filename"
I am working from an Ubuntu terminal.
Thank you!silvia_n_netsTue, 28 Jun 2016 11:14:23 -0500http://ask.sagemath.org/question/33954/How do I install python modules, or use a different version of python? (with sage)http://ask.sagemath.org/question/8927/how-do-i-install-python-modules-or-use-a-different-version-of-python-with-sage/I have several versions of python, as well as the one included with sage. I want to use the svgwrite module in /Library/Frameworks/Python.framework/Versions/2.7/bin/python , but sage does not have it.
I want to run the following (combined with some output from sage)
import svgwrite
dwg = svgwrite.Drawing('test.svg', profile='tiny')
dwg.add(dwg.line((0, 0), (10, 0), stroke=svgwrite.rgb(10, 10, 16, '%')))
dwg.add(dwg.text('Test', insert=(0, 0.2), fill='red'))
dwg.save()
It runs fine with the calls
python drawT.sage
python2.7 drawT.sage
But when I try to call it directly or with a system call in sage I have problems.
sage: import sys
sage: os.system("python /path/drawT.sage")
Traceback (most recent call last):
File "/path/drawT.sage", line 1, in <module>
import svgwrite
ImportError: No module named svgwrite
256
sage: os.system("which python")
/Applications/sage/local/bin/python
0
Okay fine, sage's installation does not have the module. But now I try to force it to use the installation that does have it:
sage: os.system("which python2.7")
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
0
sage: os.system("python2.7 /path/drawT.sage")
Traceback (most recent call last):
File "/path/drawT.sage", line 1, in <module>
import svgwrite
ImportError: No module named svgwrite
256
How can I install svgwrite, or invoke a given python installation from sage?
alejandroericksonMon, 30 Apr 2012 16:21:41 -0500http://ask.sagemath.org/question/8927/