首页 > 解决方案 > 名称“sympy”未定义

问题描述

昨天我的代码运行良好,但今天 jupyter 无法识别 sympy,我已经检查并安装了它,我该怎么办?

from sympy import expand, symbols

x0=2
y0=4

x1=5
y1=1

x =symbols("x")

L0=sympy.expand((x-x1)/(x0-x1))*y0
L1=sympy.expand((x-x0)/(x1-x0))*y1

print ("L0= ",L0)
print ("L1= ",L1)
               
print ("px= ",L0+L1)

错误堆栈跟踪:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-9cbabedcf78d> in <module>
      9 x =symbols("x")
     10 
---> 11 L0=sympy.expand((x-x1)/(x0-x1))*y0
     12 L1=sympy.expand((x-x0)/(x1-x0))*y1
     13 

NameError: name 'sympy' is not defined

标签: pythonjupytersympy

解决方案


由于您已扩展已导入,如下所示:

from sympy import expand, symbols

像这样运行这两行而不像这样使用sympy.

L0=expand((x-x1)/(x0-x1))*y0
L1=expand((x-x0)/(x1-x0))*y1

另外,以防万一在您的 jupyter notebook 中运行它并确保sympy已安装:

pip list

推荐阅读