How to go from .sage to .pyc?

26 views
Skip to first unread message

pong

unread,
Oct 16, 2019, 1:18:38 PM10/16/19
to sage-support
I have a bunch of old scripts in .sage files. They were compiled into .pyc files years ago.

Unfortunately, I only remember that I ran sage --preparse on the .sage files and got them into .py files but forgot what next.

I tried import py_compile in sage then ran py_compile.compile('xxx.py') which gave me a .pyc file but I couldn't load it or attach it to sage.

I understand one can change the file extension to .spyx and have sage to compile it by loading it into a session but I would like the pyc files by themselves. 

Can I get some help on this? 

Dima Pasechnik

unread,
Oct 16, 2019, 3:02:00 PM10/16/19
to sage-support
On Wed, Oct 16, 2019 at 6:18 PM pong <wypo...@gmail.com> wrote:
>
> I have a bunch of old scripts in .sage files. They were compiled into .pyc files years ago.

*.sage files are preparsed by sage preparser, and converted into *.py files.
Conversion into *.pyc files is done automatically, as *.py files are
loaded into Sage's Python interpreter.

One cannot load() or attach() *.pyc files, as far as I know - but why
would you need this?
(unless you want to hide the source of your *.sage scripts from the user)

HTH
Dima

>
> Unfortunately, I only remember that I ran sage --preparse on the .sage files and got them into .py files but forgot what next.
>
> I tried import py_compile in sage then ran py_compile.compile('xxx.py') which gave me a .pyc file but I couldn't load it or attach it to sage.
>
> I understand one can change the file extension to .spyx and have sage to compile it by loading it into a session but I would like the pyc files by themselves.
>
> Can I get some help on this?
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/f28af744-043a-4100-b965-ce5642887512%40googlegroups.com.

pong

unread,
Oct 16, 2019, 3:08:57 PM10/16/19
to sage-support
That's exactly what I want (hiding sources codes from users). 

I'm teaching a class where the students need to implement some algorithms on graphs but I would like to distribute those functions in advance so that they can play with it and see what's the expected output before writing their owns. 

So how can I save those pyc files generated by sage separately and have them run inside a session?
Thanks in advance



On Wednesday, October 16, 2019 at 12:02:00 PM UTC-7, Dima Pasechnik wrote:
On Wed, Oct 16, 2019 at 6:18 PM pong <wypo...@gmail.com> wrote:
>
> I have a bunch of old scripts in .sage files. They were compiled into .pyc files years ago.

*.sage files are preparsed by sage preparser, and converted into *.py files.
Conversion into *.pyc files is done automatically, as *.py files are
loaded into Sage's Python interpreter.

One cannot load() or attach() *.pyc files, as far as I know - but why
would you need this?
(unless you want to hide the source of your *.sage scripts from the user)

HTH
Dima

>
> Unfortunately, I only remember that I ran sage --preparse on the .sage files and got them into .py files but forgot what next.
>
> I tried import py_compile in sage then ran py_compile.compile('xxx.py') which gave me a .pyc file but I couldn't load it or attach it to sage.
>
> I understand one can change the file extension to .spyx and have sage to compile it by loading it into a session but I would like the pyc files by themselves.
>
> Can I get some help on this?
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-s...@googlegroups.com.

Dima Pasechnik

unread,
Oct 16, 2019, 3:37:52 PM10/16/19
to sage-support


On Wed, 16 Oct 2019, 20:09 pong, <wypo...@gmail.com> wrote:
That's exactly what I want (hiding sources codes from users). 

I'm teaching a class where the students need to implement some algorithms on graphs but I would like to distribute those functions in advance so that they can play with it and see what's the expected output before writing their owns. 

So how can I save those pyc files generated by sage separately and have them run inside a session?

I think if you make your *.sage scripts more "pytonic", so that they can be imported by Python "import" statements, then it would suffice to supply *.pyc files only.

Thanks in advance


On Wednesday, October 16, 2019 at 12:02:00 PM UTC-7, Dima Pasechnik wrote:
On Wed, Oct 16, 2019 at 6:18 PM pong <wypo...@gmail.com> wrote:
>
> I have a bunch of old scripts in .sage files. They were compiled into .pyc files years ago.

*.sage files are preparsed by sage preparser, and converted into *.py files.
Conversion into *.pyc files is done automatically, as *.py files are
loaded into Sage's Python interpreter.

One cannot load() or attach() *.pyc files, as far as I know - but why
would you need this?
(unless you want to hide the source of your *.sage scripts from the user)

HTH
Dima

>
> Unfortunately, I only remember that I ran sage --preparse on the .sage files and got them into .py files but forgot what next.
>
> I tried import py_compile in sage then ran py_compile.compile('xxx.py') which gave me a .pyc file but I couldn't load it or attach it to sage.
>
> I understand one can change the file extension to .spyx and have sage to compile it by loading it into a session but I would like the pyc files by themselves.
>
> Can I get some help on this?
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-s...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/f28af744-043a-4100-b965-ce5642887512%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/50e388e8-49e4-41f7-b042-708700c3e653%40googlegroups.com.

henri....@gmail.com

unread,
Oct 17, 2019, 2:22:45 AM10/17/19
to sage-s...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages