1

I'm using Octave on OS X and I wanted to use the symbolic package. I had to install the package running pkg install -forge symbolic on Octave, but then I also had to install sympy. I installed both mpmath and sympy running on my terminal two commands: pip install sympy and pip install mpmath.

At this point I tried to use the package launching this small script

pkg load symbolic
syms t

but it gave me this error

Symbolic pkg v2.9.0: Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sympy'
error: Python cannot import SymPy: have you installed SymPy?
    Try "sympref diagnose" for more information.
error: called from
    assert_have_python_and_sympy at line 123 column 7
    python_ipc_popen2 at line 79 column 5
    python_ipc_driver at line 62 column 15
    pycall_sympy__ at line 163 column 11
    valid_sym_assumptions at line 38 column 10
    assumptions at line 82 column 7
    syms at line 97 column 13

Then I run the command sympref diagnose as suggested, whit this output

Symbolic package diagnostics
============================

Python and SymPy are needed for most features of the Symbolic package.

The Python interpreter is currently: "python3".

Computers may have more than one Python interpreter installed.  If you
need to, you can select a different one using the PYTHON environment
variable (see "help sympref").  For example, to use Python 2, try
    setenv PYTHON python2
    sympref reset

Attempting to run python3 -c "print(\"Python says hello\")"

status = 0
output = Python says hello

Good, Python ran correctly.


Python version
--------------

Let's check what version of Python we are calling...

Attempting to run python3 -c "import sys; print(sys.version)"

status = 0
output = 3.9.4 (default, Apr 14 2021, 21:04:05)
[Clang 11.0.0 (clang-1100.0.33.17)]


SymPy Python Library
--------------------

SymPy is a Python library used by Symbolic for almost all features.

Attempting to run python3 -c "import sympy; print(sympy.__version__)"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sympy'
status = 1
output =

Unfortunately status was non-zero: probably Python cannot import sympy.

  * Is there an error message above?

  * Do you have SymPy installed?  If not, please try to install it and
    try again.

  * If you do have SymPy installed, maybe it's installed for a different
    Python interpreter than the one we found?  Please try "setenv" as
    described above to change your python interpreter.

Python3 is the right interpreter, but the version is not. If i run the very same code import sys; print(sys.version) I don't get the output Octave provides me

output = 3.9.4 (default, Apr 14 2021, 21:04:05)
    [Clang 11.0.0 (clang-1100.0.33.17)]

but I get

3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)]

May this be the problem?
I also tried to:

  • uninstall and install again both sympy and mpmath
  • install both of them using pip3 rather than pip
  • move mpmath and sympy in the same folder, which is /Users/jacopo/Library/Python/3.10/lib/python/site-packages
  • downgrade sympy package with the command pip install --user sympy==1.5.1
  • checked if both of them are installed running pip show <pkg_name>, and they are
  • restart Octave, Python and the Terminal multiple times

Output of which -a python python3 pip pip3:

/usr/bin/python
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3
/usr/local/bin/python3
/usr/bin/python3
/Library/Frameworks/Python.framework/Versions/3.10/bin/pip
/Library/Frameworks/Python.framework/Versions/3.10/bin/pip3
/usr/local/bin/pip3
/usr/bin/pip3
Jacopo
  • 99
  • 1
  • 8
  • do you have multiple python versions installed? try installing instead with `python3 -m pip install sympy`. that will use the pip associated with the default `python3`. – jkr Apr 29 '22 at 22:00
  • No, should only have `python3` Anyway I just tried to uninstall `sympy` and install it again using your code. Nothing solved :/ – Jacopo Apr 29 '22 at 22:04
  • Can you include the output of ‘which -a python python3 pip pip3” in your question? – jkr Apr 30 '22 at 13:17
  • @jakub Just included. – Jacopo May 01 '22 at 17:23

2 Answers2

0

If you're trying to install sympy globally like I was, try the below instead and see if it makes a difference (it did for me); I'm not sure why it makes any difference, but I suspect it's due to macOS-specific idiosyncrasies. Basically, instead of using pip3, try:

python3 -m pip install sympy

or

sudo python3 -m pip install sympy

After this, the Octave error went away for me. (Also, I'm using macOS Monterey.)

Credit goes to this post: https://stackoverflow.com/a/58224907

user2524973
  • 1,087
  • 6
  • 14
0

You have multiple versions of Python installed, in your shell, python3 is a different interpreter than what Octave finds when it does python3. If you install a package through the shell, it will not be available to the Python interpreter that Octave finds.

You can tell Octave which Python interpreter to use. For example, in your shell, do

which python3

which might say

/usr/local/bin/python3

This is the Python interpreter for which you installed the package.

Then you’d do

export PYTHON /usr/local/bin/python3
octave

to start Octave.

Alternatively, from within Octave, as indicated in the diagnostic message added to the question above,

setenv PYTHON /usr/local/bin/python3
sympref reset

Reference: Octave docs for sympref.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120