Virtual Environment to use Octave with Python3 packages on masOS

For anyone having the same issue:

I was running

octave --gui

directly from the terminal with macOS system 15.3.1. Running a script utilizing the symbolic package yields the below error:

Symbolic pkg v3.2.1: Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import sympy; print(sympy.__version__)
    ^^^^^^^^^^^^
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 135 column 7
    python_ipc_popen2 at line 87 column 5
    python_ipc_driver at line 62 column 15
    pycall_sympy__ at line 164 column 11
    valid_sym_assumptions at line 38 column 10
    assumptions at line 82 column 7
    syms at line 140 column 13
    CompleExp at line 5 column 1

If homebrew was used to install Python, follow the steps described here to set up a virtual environment and install the sympy package inside the virtual environment. Once completed, inside the virtual environment, run below:

octave – gui

The octave gui will pop up. Type the following in the Octave gui command window:

sympref diagnose

the following will pop up now showing sympy is functional:

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 3, try
    setenv PYTHON python3
    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.13.2 (main, Feb  4 2025, 14:51:09) [Clang 16.0.0 (clang-1600.0.26.6)]

Running python3 -c "import sys; print(sys.executable)"
output = /Users/ashhen/.venv/bin/python3

Checking if python3 is running in a Cygwin-like POSIX environment... no


System info
-----------

Platform: aarch64-apple-darwin23.6.0
Array Maximum Size: 9223372036854775807
Endianness: L
Architecture: darwin23.6.0-aarch64
----------------------------------------------------------------------
GNU Octave Version: 9.4.0 (hg id: df465cbba05a)
GNU Octave License: GNU General Public License
Operating System: Darwin 24.3.0 Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:16 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6000 arm64
----------------------------------------------------------------------
Package Name  | Version | Installation directory
--------------+---------+-----------------------
    symbolic *|   3.2.1 | /Users/ashhen/.local/share/octave/api-v59/packages/symbolic-3.2.1


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__)"

status = 0
output = 1.13.3

SymPy must be at least version 1.5.1; you have version 1.13.3.

Good, a working version of SymPy is installed.


Python XML Parsing and DOM Support
----------------------------------

The XML DOM library is used by Symbolic for passing values to and from Python.
Some older versions of Python formatted XML output differently.  As long as you
have any reasonably recent version of Python, this should pass.

Attempting to run python3 -c "import xml.dom.minidom as minidom; doc = minidom.parseString(\"<item>value</item>\"); print(doc.toprettyxml(indent=\"\"))"

status = 0
output = <?xml version="1.0" ?>
<item>value</item>



Your kit looks good for running the Symbolic package.  Happy hacking!

Hope this helps.