Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

python - my jupyter notebook fails to import anaconda modules consistently

I can't get my jupyter notebook properly import any modules. The weird thing is, I am able to import numpy but not pandas with Sublime.

I wiped out every python related content on my Mac and re-installed anaconda. Here's the import error message for numpy on jupyter:

ImportError                               Traceback (most recent call last)
<ipython-input-1-5a0bd626bb1d> in <module>()
----> 1 import numpy

/Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/__init__.pyc in <module>()
    183         return loader(*packages, **options)
    184 
--> 185     from . import add_newdocs
    186     __all__ = ['add_newdocs',
    187                'ModuleDeprecationWarning',

/Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/add_newdocs.py in <module>()
     11 from __future__ import division, absolute_import, print_function
     12 
---> 13 from numpy.lib import add_newdoc
     14 
     15 ###############################################################################

/Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/lib/__init__.py in <module>()
     16 
     17 from . import scimath as emath
---> 18 from .polynomial import *
     19 #import convertcode
     20 from .utils import *

/Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/lib/polynomial.py in <module>()
     17 from numpy.lib.function_base import trim_zeros, sort_complex
     18 from numpy.lib.type_check import iscomplex, real, imag
---> 19 from numpy.linalg import eigvals, lstsq, inv
     20 
     21 class RankWarning(UserWarning):

/Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/__init__.py in <module>()
     49 from .info import __doc__
     50 
---> 51 from .linalg import *
     52 
     53 from numpy.testing import Tester

/Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/linalg.py in <module>()
     27     )
     28 from numpy.lib import triu, asfarray
---> 29 from numpy.linalg import lapack_lite, _umath_linalg
     30 from numpy.matrixlib.defmatrix import matrix_power
     31 from numpy.compat import asbytes

ImportError: dlopen(/Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/lapack_lite.so, 2): Library not loaded: @rpath/lib/libmkl_intel_lp64.dylib
  Referenced from: /Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/lapack_lite.so
  Reason: image not found

And here's the message for pandas:

ImportError                               Traceback (most recent call last)
<ipython-input-5-d6ac987968b6> in <module>()
----> 1 import pandas

/Users/z-wang/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/__init__.py in <module>()
     11                       "pandas from the source directory, you may need to run "
     12                       "'python setup.py build_ext --inplace' to build the C "
---> 13                       "extensions first.".format(module))
     14 
     15 from datetime import datetime

ImportError: C extension: scimath not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Deleting everything Python related is a bad idea. There are system files which require it. Hopefully you didn't delete the wrong files and won't have to reinstall your operating system.

Regarding your question, you need to first activate your conda environment before launching jupyter notebook.

To see which environment you already have installed, type the following from a terminal window:

conda info --envs

Then type the following to activate your environment (obviously my_env is the name of what ever your environment is).

source activate my_env

From here, you are in your conda environment. To open Jupyter notebook, just type:

jupyter notebook

This notebook will be linked to your conda environment and will have access to all modules therein (type conda list from the terminal once you've activated your environment per above to see them, or !conda list from within the notebook).

Just for fun, we'll create a quick environment named test_env.

conda create -n test_env pandas jupyter notebook qtconsole matplotlib
source activate test_env
jupyter notebook  # launches your notebook

Alternatively, to launch qtconsole:

jupyter qtconsole

Although we only installed a few packages, all linked dependencies are also installed (including numpy). This is now the output of conda list:

$ conda list
# packages in environment at /usr/local/miniconda/envs/test_env:
#
appnope                   0.1.0                    py27_0    defaults
backports-abc             0.4                       <pip>
backports.ssl-match-hostname 3.4.0.2                   <pip>
backports_abc             0.4                      py27_0    defaults
cycler                    0.10.0                   py27_0    defaults
decorator                 4.0.9                    py27_0    defaults
freetype                  2.5.5                         0    defaults
ipykernel                 4.3.1                    py27_0    defaults
ipython                   4.1.2                    py27_0    defaults
ipython-genutils          0.1.0                     <pip>
ipython_genutils          0.1.0                    py27_0    defaults
ipywidgets                4.1.1                    py27_0    defaults
jinja2                    2.8                      py27_0    defaults
jsonschema                2.4.0                    py27_0    defaults
jupyter                   1.0.0                    py27_1    defaults
jupyter-client            4.1.1                     <pip>
jupyter-console           4.1.0                     <pip>
jupyter-core              4.0.6                     <pip>
jupyter_client            4.1.1                    py27_0    defaults
jupyter_console           4.1.0                    py27_0    defaults
jupyter_core              4.0.6                    py27_0    defaults
libpng                    1.6.17                        0    defaults
markupsafe                0.23                     py27_0    defaults
matplotlib                1.5.1               np110py27_0    defaults
mistune                   0.7.1                    py27_0    defaults
mkl                       11.3.1                        0    defaults
nbconvert                 4.1.0                    py27_0    defaults
nbformat                  4.0.1                    py27_0    defaults
notebook                  4.1.0                    py27_0    defaults
numpy                     1.10.4                   py27_0    defaults
openssl                   1.0.2g                        0    defaults
pandas                    0.17.1              np110py27_0    defaults
path.py                   8.1.2                    py27_1    defaults
pexpect                   3.3                      py27_0    defaults
pickleshare               0.5                      py27_0    defaults
pip                       8.0.3                    py27_0    defaults
ptyprocess                0.5                      py27_0    defaults
pygments                  2.1.1                    py27_0    defaults
pyparsing                 2.0.3                    py27_0    defaults
pyqt                      4.11.4                   py27_1    defaults
python                    2.7.11                        0    defaults
python-dateutil           2.4.2                    py27_0    defaults
python.app                1.2                      py27_4    defaults
pytz                      2015.7                   py27_0    defaults
pyzmq                     15.2.0                   py27_0    defaults
qt                        4.8.7                         1    defaults
qtconsole                 4.1.1                    py27_0    defaults
readline                  6.2                           2    <unknown>
setuptools                20.1.1                   py27_0    defaults
simplegeneric             0.8.1                    py27_0    defaults
singledispatch            3.4.0.3                  py27_0    defaults
sip                       4.16.9                   py27_0    defaults
six                       1.10.0                   py27_0    defaults
sqlite                    3.9.2                         0    defaults
ssl_match_hostname        3.4.0.2                  py27_0    defaults
terminado                 0.5                      py27_1    defaults
tk                        8.5.18                        0    http://repo.continuum.io/pkgs/free/osx-64/tk-8.5.18-0.tar.bz2
tornado                   4.3                      py27_0    defaults
traitlets                 4.1.0                    py27_0    defaults
wheel                     0.29.0                   py27_0    defaults
zlib                      1.2.8                         0    <unknown>

Once done, deactivate the environment.

source deactivate  # From within the terminal of the active environment.

If you'd like to delete it:

conda env remove -n test_env

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...