Sage cannot be launched if `python3` does not point to system interpreter

Bug #1960459 reported by James Gerity
16
This bug affects 3 people
Affects Status Importance Assigned to Milestone
sagemath (Ubuntu)
Confirmed
Undecided
Unassigned

Bug Description

## Expected behavior:

Running `sage` after installing the `sagemath` command starts Sage, even if `python3` resolves somewhere other than `/usr/bin/python3` (examples of when it doesn't: virtual environments, use of the conda package manager, pyenv)

## Actual behavior:

```
$ sage
Traceback (most recent call last):
  File "/usr/share/sagemath/bin/sage-ipython", line 9, in <module>
    from sage.misc.banner import banner
ModuleNotFoundError: No module named 'sage'
```

## Reproduction:

```
$ sudo apt install sagemath
...
$ which python3
/usr/bin/python3
$ sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.0, Release Date: 2020-01-01 │
│ Using Python 3.8.10. Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: 1 + 1
2
sage:
Exiting Sage (CPU time 0m0.14s, Wall time 0m27.42s).
$ python3 -m venv --copies /tmp/testvenv # note: --copies is necessary for full isolation
$ source activate /tmp/testvenv/bin/activate
$ which python3
/tmp/testvenv/bin/python3
$ sage
Traceback (most recent call last):
  File "/usr/share/sagemath/bin/sage-ipython", line 9, in <module>
    from sage.misc.banner import banner
ModuleNotFoundError: No module named 'sage'
```

As a workaround for anybody else who stumbles into this, you can still invoke sage as: `/usr/bin/python3 /usr/share/sagemath/bin/sage-ipython`, which forces

## Additional information

There are a few layers of indirection at work in this bug, but at the end of the day, this happens because of an `exec python3` embedded in the part of the `sage` shell script responsible for launching Python. Upstream reference: https://github.com/sagemath/sage/blob/9.0/src/bin/sage#L614-L631

If I'm not mistaken, these lines should always point to /usr/bin/python3 for the Ubuntu package to avoid this flaw. Note that there are other executables (ipython in particular) which may be appropriate to locate against the system as well. The package maintainer(s) probably know how to fix this better than I do, but I can confirm that replacing `exec python3` with `exec /usr/bin/python3` in these two locations does appear to resolve the problem.

## Boilerplate information:

1) system info

$ lsb_release -rd
Description: Ubuntu 20.04.3 LTS
Release: 20.04

2) package info

$ apt-cache policy sagemath
sagemath:
  Installed: 9.0-1ubuntu4
  Candidate: 9.0-1ubuntu4
  Version table:
 *** 9.0-1ubuntu4 500
        500 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 Packages
        100 /var/lib/dpkg/status

Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in sagemath (Ubuntu):
status: New → Confirmed
Revision history for this message
Jaakko Kuurne (jjaakko) wrote (last edit ):

I am experiencing this issue as well on Ubuntu 20.04.4. Did not find a way to run sage notebooks. Also, sage doesn't seem to provide binaries for linux anymore: https://www.sagemath.org/download-linux.html

Revision history for this message
James Gerity (snoopj) wrote :

Looking at this again, I believe this bug may have been fixed (or at least mitigated) in Sage 9.1, which introduced some changes to the `sage` entrypoint around the `exec` commands I identified in my original report. More specifically the changes introduced by this commit: https://github.com/sagemath/sage/commit/248c13b542b12521bd91d6648bd6220fb7593a26 (tied to this ticket on the sagemath tracker: https://trac.sagemath.org/ticket/29627)

If I change `exec python3` in `/usr/bin/sage` to `exec "$SAGE_PYTHON"/bin/python3` (i.e. apply the changes in the linked commit), sage does load up successfully even if a virtual environment is active (i.e. if `python3` points somewhere other than `/usr/bin/python3`). IPython does still issue a warning about the active venv, but sage is able to successfully start. Not sure if there is any interest in a backport here, or even if this package is under active maintenance in Ubuntu, but thought I'd share some more information about the problem upstream.

I should also note that on 22.04 (sage 9.5), sage does not start when a venv is active, although the error report is substantially longer and it appears to be a separate issue.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.