Ask Your Question
0

Adding Code To My SageMath Notebook

asked 2014-02-17 14:11:05 -0600

russ_hensel gravatar image

updated 2015-01-13 11:21:28 -0600

FrédéricC gravatar image

I have written a class, “LRC”, using the Sage Notebook which works just fine. Now I would like to put it into my sage environment without including it on each worksheet.

Before I go on I should describe my environment a bit. I am running a windows 7 machine with Virtual Box. I imported a Sage Notebook virtual machine and run it. The virtual box shares some of the drive space with the windows machine, in particular a spot called D:\unixshare

So I extracted the code from the cell to a file and placed it at D:\unixshare\forLRC\LRC.py which at least in theory available to the virtual box machine.

In a new worksheet I put this in a cell.

import sys sys.path.append('/media/sf_D_DRIVE//unixshare/forLRC') # move your python files here

import LRC

lrc = LRC()

.-.-.-.-.-.-.-.-.-.-

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "_sage_input_4.py", line 10, in <module>

exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aW1wb3J0IHN5cwpzeXMucGF0aC5hcHBlbmQoJy9tZWRpYS9zZl9EX0RSSVZFLy91bml4c2hhcmUvZm9yTFJDJykgIyBtb3ZlIHlvdXIgcHl0aG9uIGZpbGVzIGhlcmUKCmltcG9ydCBMUkMKCmxyYyAgICA9IExSQygp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))

File "", line 1, in <module>

File "/tmp/tmpMF8IdA/___code___.py", line 5, in <module> import LRC File "/media/sf_D_DRIVE//unixshare/forLRC/LRC.py", line 22, in <module> class LRC( object ): File "/media/sf_D_DRIVE//unixshare/forLRC/LRC.py", line 31, in LRC var( "myOmega" ) # sometimes used as the angular frequency

NameError: name 'var' is not defined

I did get a new file: D:\unixshare\forLRC\LRC.pyc

So I have a problem that does not let me go any farther. I am not a particularly experienced python programmer, but have programmed in various languages for some time. Some help? Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2014-02-18 04:20:23 -0600

russ_hensel gravatar image

updated 2014-02-18 05:50:08 -0600

# Answering my own question: this seems to make it work
# put this in the LRC.pyFile before any code "from sage.all import *"

import sys
sys.path.append('/media/sf_D_DRIVE//unixshare/forLRC') # move your python files here


import LRC

from LRC import *

# try it out
lrc    = LRC()
print lrc

And it printed what I expected

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-02-17 14:11:05 -0600

Seen: 163 times

Last updated: Feb 18 '14