3-manifolds / Sage_macOS

SageMath as a macOS application bundle.
152 stars 15 forks source link

SageMath-9-7 can not be launched in a TeXmacs sage session #48

Closed Matthias-A-Steiner closed 1 year ago

Matthias-A-Steiner commented 1 year ago

I’m on mac OSX 13.6/15.7 and currently using TeXmacs 2.1.2. I’ve recently upgraded from SageMath-9-4 (which worked perfectly fine on these systems) to SageMath-9-7.

When I want to launch a Sage session in TeXmacs now, I get the following error message:

Traceback (most recent call last):
File “/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs/plugins/tmpy/session/tm_sage.py”, line 16, in 
tmpy_home_path = os.environ.get(“TEXMACS_HOME_PATH”) + “/plugins/tmpy”
TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’

and the session freezes.

I was being told that the error message means that os.environ.get(“TEXMACS_HOME_PATH”)is returning None, i.e. it is not defined, so Python is running outside the shell which called TeXmacs.

This problem did not occur with SageMath-9-4.

So, comparing the directories /private/var/tmp/sage-9.4-current with /private/var/tmp/sage-9.7-current , I noticed that there is a softlinked subdirectory venv in sage-9.7 which is linked to local/var/lib/sage/venv-python3.10.5/which itself is linked to /Applications/SageMath-9-7.app/Contents/Frameworks/Sage.framework/Versions/9.7/local/var/lib/sage/venv-python3.10.5.

Could it be that these multiple softlinks prevent launching SageMath-9-7 from within a TeXmacs sage session? If so, how could the issue be fixed?

Any suggestions appreciated.

Cheers Tilda

culler commented 1 year ago

Thanks. There is a fix for this. An enhancement that would handle this more conveniently was planned for SageMath-9.7, but it didn't happen before the release.

First, to answer your question, the venv is part of the design of SageMath 9.7. It did not exist in 9.4. It has nothing to do with this problem.

The change between 9.4 and 9.7 which does affect this is that in 9.4 the environment in the SageMath app included everything in the user's environment at the time sage was launched. That was causing problems for people who had old copies of Sage lying around on their system and had set up environment variables to deal with those. Those environment variables could break the SageMath app by causing it to import imodules from old versions of Sage. This change was to arrange that the SageMath app launches with a minimal environment.

You can set additional environment variables by creating an iPython startup file. (Thanks to Samuel Lelièvre for this information.)

In your case, you could create a file in the following subdirectory of your .sage directory: $HOME/.sage/ipython-5.0.0/profile_default/startup which sets up your environment. For example, you could create a file named: $HOME/.sage/ipython-5.0.0/profile_default/startup/10-environ.py containing two lines similar to these: import os os.environ['TEXMACS_HOME_PATH'] = '/path/to/your/texmacs/home

Multiple startup files are allowed and they are run in lexicographical order before ipython starts up, as explained in the README file in the startup directory. That README file suggests using numerical prefixes (e.g. 10-) to control the order.

Hopefully the next release will allow you to edit your environment within the gui that starts the SageMath app.

Matthias-A-Steiner commented 1 year ago

Dear Marc, thanks for your fast reply and the detailed explanation. I created the file 10-envrion.py in the suggested directory putting these two lines in it:

import os os.environ['TEXMACS_HOME_PATH'] = '/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs' I also tried import os os.environ['TEXMACS_PATH'] = '/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs'

Yet, in both cases the sage session in TeXmacs produced the same error message as before to eventually freeze. Maybe I got the Python syntax wrong, maybe the TeXmacs sage plugin in not compatible with SageMath-9-7. I guess I'll have to reinstall SageMath-9-4 till the new release (SageMath-9-8?) is out.

Cheers Tilda

culler commented 1 year ago

Reinstalling 9.4 would be silly.

Please start your SageMath-9.7 app in the terminal and print out os.environ to see whether the variables have been correctly set. The error message is telling you which environment variable is not set. Check if it has been set in your os.environ.

There should be two lines in the file. It looks like you only have one (without a semicolon) from what you wrote in the comment.

Matthias-A-Steiner commented 1 year ago

Thanks again. I restarted my ageMath-9.7 app in the terminal and printed out os.environ. Judging from the output

environ{ . . . 'TEXMACS_PATH': '/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs'}

the environment obviously seems to be correctly set. But the error message prevails, even though my 10-envrion.py is a two-liner. Even after removing all visible traces of SageMath-9-4.app from my system, I still get the old error message in the TeXmacs session.

culler commented 1 year ago

Judging from your first message the name of the environment variable is TEXMACS_HOME_PATH but it looks like the variable that you set in your startup file was named TEXMACS_PATH. I think you should try TEXMACS_HOME_PATH instead.

Matthias-A-Steiner commented 1 year ago

I've tried that, but it did not make any difference. The TeXmacs sage plugin tm_sage.py is in TEXMACS_PATH which is /Applications/TeXmacs.app/Contents/Resources/share/TeXmacs + /plugins/tmpy + /session. Setting the environment to TEXMACS_HOME_PATH redirects to user specific reconfigurations in case they're done. They are read in first, if there are none, TEXMACS_PATH is evaluated. From tm_sage.py:

import os
import sys
from os.path import exists
tmpy_home_path = os.environ.get("TEXMACS_HOME_PATH") + "/plugins/tmpy"
if (exists (tmpy_home_path)):
    sys.path.append(os.environ.get("TEXMACS_HOME_PATH") + "/plugins/")
else:
    sys.path.append(os.environ.get("TEXMACS_PATH") + "/plugins/")

For SageMath-9-4 this worked well.

Matthias-A-Steiner commented 1 year ago

Dear Marc, thanks for your suggestions so far. Would you consider the configuration issue SageMath specific or related to the sage-plugin in TeXmacs? If the latter is the case I could, if this is all right with you, refer to our exchange on the TeXmacs forum.

Tilda

culler commented 1 year ago

I am planning to install TeXmacs and investigate what happens on my system. But I haven't gotten to it yet. Sorry.

Matthias-A-Steiner commented 1 year ago

very kind of you

culler commented 1 year ago

I installed TeXmacs and my conclusion is that this is not related to Sage. TeXmacs is not correctly initializing its environment. This is made more complicated by the fact that they are using python 2.7 internally, even though they only support python 3 sessions. (Their shebang is: #! /usr/bin/env python which gives python 2.7 on macOS.)

I was able to get the sage sessions to work by editing the files tm_sage.py and postscript.py inside of the app bundle. But this is really something that TeXmacs should fix, in my opinion. Also this is unrelated to the sage environment, although I still do plan to add the ability to change the sage environment.

Matthias-A-Steiner commented 1 year ago

Thanks for investigating. It's good to hear that you got SageMath-9-7 running in TeXmacs sage session . May I ask what exactly you had to tweak in tm_sage.py and postscript.py.

The python set-up in TeXmacs is indeed a bit confusing. When I run /usr/bin/env python

I suppose you're quite right that "TeXmacs is not correctly initializing its environment. This is made more complicated by the fact that they are using python 2.7 internally, even though they only support python 3 sessions."

Cheers, Tilda

culler commented 1 year ago

I think the simplest hack to make a sage session work is to just set the two environment variables to their expected values in the file: /Applications/TeXmacs.app/Contents/Resources/share/TeXmacs/plugins/tmpy/session/tm_sage.py But doing this makes it impossible to relocate the app and it really should be handled in the app's startup code. I hope you will raise an issue at the TeXmacs project.

--- tm_sage.py.orig 2022-11-20 17:49:33
+++ tm_sage.py  2022-11-21 08:04:38
@@ -13,6 +13,8 @@
 import os
 import sys
 from os.path import exists
+os.environ["TEXMACS_HOME_PATH"] = os.path.join(os.environ["HOME"], ".TeXmacs")
+os.environ["TEXMACS_PATH"] = "/Applications/TeXmacs.app/Contents/Resources/share/TeXmacs"
 tmpy_home_path = os.environ.get("TEXMACS_HOME_PATH") + "/plugins/tmpy"
 if (exists (tmpy_home_path)):
     sys.path.append(os.environ.get("TEXMACS_HOME_PATH") + "/plugins/")
culler commented 1 year ago

I think this can be closed now.