Python无法导入SymPy:你安装了SymPy了吗?

问题描述 投票:0回答:2

我在 OS X 上使用 Octave,我想使用符号包。 我必须在 Octave 上安装运行

pkg install -forge symbolic
的包,但随后我还必须安装
sympy
。我安装了
mpmath
sympy
在我的终端上运行两个命令:
pip install sympy
pip install mpmath
.

此时我尝试使用启动这个小脚本的包

pkg load symbolic
syms t

但是它给了我这个错误

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

然后我按照建议运行命令

sympref diagnose
,得到这个输出

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 是正确的解释器,但版本不是。如果我运行完全相同的代码

import sys; print(sys.version)
我没有得到Octave提供给我的输出

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

但我明白了

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

这可能是问题所在吗?
我也尝试过:

  • 卸载并重新安装 sympy 和 mpmath
  • 使用 pip3 而不是 pip 安装它们
  • 将mpmath和sympy放在同一个文件夹下,即
    /Users/jacopo/Library/Python/3.10/lib/python/site-packages
  • 使用命令降级sympy包
    pip install --user sympy==1.5.1
  • 检查它们是否都安装运行
    pip show <pkg_name>
    ,它们是
  • 多次重启 Octave、Python 和终端

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
python-3.x octave sympy
2个回答
0
投票

如果你想像我一样在全球范围内安装 sympy,试试下面的方法,看看它是否有所作为(它对我有用);我不确定为什么会有所不同,但我怀疑这是由于 macOS 特定的特质所致。基本上,而不是使用

pip3
,尝试:

python3 -m pip install sympy

sudo python3 -m pip install sympy

在此之后,八度错误对我来说消失了。 (此外,我正在使用 macOS Monterey。)

信用转到这篇文章:https://stackoverflow.com/a/58224907


0
投票

你安装了多个版本的 Python,在你的 shell 中,

python3
是一个不同于 Octave 发现的解释器
python3
。如果你通过 shell 安装一个包,Octave 找到的 Python 解释器将无法使用它。

你可以告诉 Octave 使用哪个 Python 解释器。例如,在你的 shell 中,做

which python3

这可能会说

/usr/local/bin/python3

这是您为其安装包的 Python 解释器。

然后你会做

export PYTHON /usr/local/bin/python3
octave

开始Octave.

或者,从 Octave 内部,如添加到上述问题的诊断消息中所示,

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

参考:Octave docs for

sympref
.

© www.soinside.com 2019 - 2024. All rights reserved.