在 IPython 中导入 sympy 和 scipy 时遇到的错误及其解决方法

51 阅读2分钟

在 Mac OS X 10.9 上使用 IPython 无法导入 sympy 和 scipy。尝试在 Mac 终端使用 IPython 导入这两个库,但遇到了如下错误:

In [3]: import sympy
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-7fb0c7cf9177> in <module>()
----> 1 import sympy

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sympy/__init__.py in <module>()
     68 # This module is slow to import:
     69 #from physics import units
---> 70 from plotting import plot, Plot, textplot, plot_backends, plot_implicit
     71 from printing import pretty, pretty_print, pprint, pprint_use_unicode, \
     72     pprint_try_use_unicode, print_gtk, print_tree, pager_print, TableForm

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sympy/plotting/__init__.py in <module>()
----> 1 from plot import plot_backends
      2 from plot_implicit import plot_implicit
      3 from proxy_pyglet import Plot
      4 from textplot import textplot
      5 from pygletplot import PygletPlot

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sympy/plotting/plot.py in <module>()
     29 from sympy.core.compatibility import set_union
     30 import warnings
---> 31 from experimental_lambdify import (vectorized_lambdify, lambdify)
     32
     33 #TODO probably all of the imports after this line can be put inside function to

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sympy/plotting/experimental_lambdify.py in <module>()
     71
     72 from sympy.external import import_module
---> 73 np = import_module('numpy')
     74 import warnings
     75

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sympy/external/importtools.py in import_module(module, min_module_version, min_python_version, warn_not_installed, warn_old_version, module_version_attr, module_version_attr_call_args, __import__kwargs, catch)
    121
    122     try:
--> 123         mod = __import__(module, **__import__kwargs)
    124     except ImportError:
    125         if warn_not_installed:

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/__init__.py in <module>()
    141         return loader(*packages, **options)
    142
--> 143     import add_newdocs
    144     __all__ = ['add_newdocs']
    145

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/add_newdocs.py in <module>()
      7 #       core/fromnumeric.py, core/defmatrix.py up-to-date.
      8
----> 9 from numpy.lib import add_newdoc
     10
     11 ###############################################################################

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/lib/__init__.py in <module>()
     11
     12 import scimath as emath
---> 13 from polynomial import *
     14 #import convertcode
     15 from utils import *

/Users/nick/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/lib/polynomial.py in <module>()
      9 import re
     10 import warnings
---> 11 import numpy.core.numeric as NX
     12
     13 from numpy.core import isscalar, abs, finfo, atleast_1d, hstack, dot

AttributeError: 'module' object has no attribute 'core'
  1. 解决方案

为了解决这个问题,需要确保 NumPy 已正确安装。可以通过以下步骤进行安装:

pip install numpy

如果 NumPy 已安装,可以尝试以下方法:

import sys
sys.path.append('/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/')

这将把 NumPy 的安装路径添加到 Python 的搜索路径中。

然后,就可以在 IPython 中导入 sympy 和 scipy:

import sympy
import scipy