简体   繁体   中英

Why does my jupyter kernel die running in a virtual environment when importing numpy?

I am working in a virtual environment that I set up using conda. What I would like to do is open up a new Jupyter notebook and work within the notebook using this virtual environment. However, I think I am missing some sort of necessary package within my virtual environment.

When I launch Jupyter notebook from Terminal (currently using OSX 10.13.6), I can see the option to open a new notebook with the Python (virtualEnvironment) in the New button at the top right of the screen.

When I open a new notebook as Python (virtualEnvironment), everything looks good, so I try to run the following in the first cell:

import numpy as np

I get the following error message in Jupyter as a pop-up window:

Kernal restarting: The kernel appears to have died. It will restart automatically.

I know that some aspects of the Jupyter notebook are working because I can run the following in the first cell, no problem:

from IPython.display import display, Math

display(Math('\\text{This is latex formatting:} \\quad x + 2y = 3j + 4'))

Does anyone have any ideas about what might be causing the kernel to die?

Here are all of the packages in the virtual envronment:

# packages in environment at /opt/anaconda3/envs/virtualEnvironment:
#
# Name                    Version                   Build  Channel
appnope                   0.1.0                    py37_0  
attrs                     19.3.0                     py_0    conda-forge
backcall                  0.1.0                    py37_0  
blas                      1.0                         mkl  
bleach                    3.1.5              pyh9f0ad1d_0    conda-forge
brotlipy                  0.7.0           py37h9bfed18_1000    conda-forge
ca-certificates           2020.4.5.1           hecc5488_0    conda-forge
certifi                   2020.4.5.1       py37hc8dfbb8_0    conda-forge
cffi                      1.14.0           py37h356ff06_0    conda-forge
chardet                   3.0.4           py37hc8dfbb8_1006    conda-forge
cryptography              2.9.2            py37he655712_0    conda-forge
decorator                 4.4.2                      py_0  
defusedxml                0.6.0                      py_0    conda-forge
entrypoints               0.3                      py37_0  
idna                      2.9                        py_1    conda-forge
importlib-metadata        1.6.0            py37hc8dfbb8_0    conda-forge
importlib_metadata        1.6.0                         0    conda-forge
intel-openmp              2019.4                      233  
ipykernel                 5.1.4            py37h39e3cac_0  
ipython                   7.13.0           py37h5ca1d4c_0  
ipython_genutils          0.2.0                    py37_0  
jedi                      0.17.0                   py37_0  
jinja2                    2.11.2             pyh9f0ad1d_0    conda-forge
json5                     0.9.0                      py_0    conda-forge
jsonschema                3.2.0            py37hc8dfbb8_1    conda-forge
jupyter_client            6.1.3                      py_0  
jupyter_core              4.6.3                    py37_0  
jupyterlab                2.1.2                      py_0    conda-forge
jupyterlab_server         1.1.1                      py_0    conda-forge
libcxx                    4.0.1                hcfea43d_1  
libcxxabi                 4.0.1                hcfea43d_1  
libedit                   3.1.20181209         hb402a30_0  
libffi                    3.2.1             h6de7cb9_1006    conda-forge
libgfortran               3.0.1                h93005f0_2  
libsodium                 1.0.16               h3efe00b_0  
markupsafe                1.1.1            py37h9bfed18_1    conda-forge
mistune                   0.8.4           py37h9bfed18_1001    conda-forge
mkl                       2019.4                      233  
mkl-service               2.3.0            py37hfbe908c_0  
mkl_fft                   1.0.15           py37h5e564d8_0  
mkl_random                1.1.0            py37ha771720_0  
nbconvert                 5.6.1            py37hc8dfbb8_1    conda-forge
nbformat                  5.0.6                      py_0    conda-forge
ncurses                   6.2                  h0a44026_1  
notebook                  6.0.3                    py37_0    conda-forge
numpy                     1.18.1           py37h7241aed_0  
numpy-base                1.18.1           py37h6575580_1  
openssl                   1.1.1g               h0b31af3_0    conda-forge
packaging                 20.1                       py_0    conda-forge
pandas                    1.0.3            py37h6c726b0_0  
pandoc                    2.9.2.1                       0    conda-forge
pandocfilters             1.4.2                      py_1    conda-forge
parso                     0.7.0                      py_0  
patsy                     0.5.1                    py37_0  
pexpect                   4.8.0                    py37_0  
pickleshare               0.7.5                    py37_0  
pip                       20.0.2                   py37_1  
prometheus_client         0.7.1                      py_0    conda-forge
prompt-toolkit            3.0.4                      py_0  
prompt_toolkit            3.0.4                         0  
ptyprocess                0.6.0                    py37_0  
pycparser                 2.20                       py_0    conda-forge
pygments                  2.6.1                      py_0  
pyopenssl                 19.1.0                     py_1    conda-forge
pyparsing                 2.4.7              pyh9f0ad1d_0    conda-forge
pyrsistent                0.16.0           py37h9bfed18_0    conda-forge
pysocks                   1.7.1            py37hc8dfbb8_1    conda-forge
python                    3.7.7           hc70fcce_0_cpython  
python-dateutil           2.8.1                      py_0  
python_abi                3.7                     1_cp37m    conda-forge
pytz                      2020.1                     py_0  
pyzmq                     18.1.1           py37h0a44026_0  
readline                  8.0                  h1de35cc_0  
requests                  2.23.0             pyh8c360ce_2    conda-forge
scipy                     1.2.1            py37h1410ff5_0  
send2trash                1.5.0                      py_0    conda-forge
setuptools                46.1.3                   py37_0  
six                       1.14.0                   py37_0  
sqlite                    3.31.1               h5c1f38d_1  
statsmodels               0.11.0           py37h1de35cc_0  
terminado                 0.8.3            py37hc8dfbb8_1    conda-forge
testpath                  0.4.4                      py_0    conda-forge
tk                        8.6.8                ha441bb4_0  
tornado                   6.0.4            py37h1de35cc_1  
traitlets                 4.3.3                    py37_0  
urllib3                   1.25.9                     py_0    conda-forge
wcwidth                   0.1.9                      py_0  
webencodings              0.5.1                      py_1    conda-forge
wheel                     0.34.2                   py37_0  
xz                        5.2.5                h1de35cc_0  
zeromq                    4.3.1                h0a44026_3  
zipp                      3.1.0                      py_0    conda-forge
zlib                      1.2.11               h1de35cc_3  

I previously tried to solve this problem using the answers from this question: Conda environments not showing up in Jupyter Notebook . I cannot figure out what the problem is in my current situation.

Other information that might be valuable: if I start a new Jupyter notebook in my normal environment, numpy imports just fine. This is the first moment when I have had a problem with the kernel dying. I have never built a virtual environment from scratch like this before; I installed ipython, ipykernel, and jupyter using conda via Terminal. I installed statsmodels using pip. Maybe using pip was my mistake?


Could this be a PATH issue? I had downloaded the GUI version of anaconda before. When I prompt terminal

echo $PATH

These paths are printed:

/Users/RZ/anaconda3/bin:
/opt/anaconda3/condabin:
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin:
/usr/texbin:
/opt/X11/bin:
/usr/local/git/bin

More recently I have performed the following:

when I run: jupyter nbextension list

I get this as an output:

Known nbextensions:
  config dir: /Users/Rentazilla/anaconda3/etc/jupyter/nbconfig
    tree section
      nb_conda/tree disabled

Is this the issue? maybe nb_conda/tree was disabled incorrectly. According to at least one link on the Jupyter notebook github ( https://github.com/jupyter/notebook/issues/1716 ) both nbextension list and serverextension should be disabled.

5/14/20

All right, so after many hours of trying a million different things, I came up with a solution that is working but I don't think its very pretty.

Not sure what to do yesterday, I decided to uninstall and reinstall conda. I used the full method detailed at https://docs.anaconda.com/anaconda/install/uninstall/ and uninstalled all anaconda3 folders and reinstalled conda using the latest miniconda installer.

Then I made a new conda environment:

conda create -n py3 ipykernel ipython jupyter_client jupyter_core traitlets ipython_genutils

Next I installed nb_conda_kernels in the base environment. Then I proceeded to install numpy, sympy, matplotlib, stats models, and jupyter_lab in the py3 environment.

I noticed that jupyter still sees some old environments as kernels; I need to get rid of them. For now I am only keeping the base environment and the py3 environment

I noticed that jupyter was using the wrong path for its kernels so I changed that

This did not fix the issue. I down graded python to 3.7.7 on the base and py3 environments. That did not fix the issue either. So I tried running some basic commands in python using the terminal. I found that running import numpy caused python to crash and gave the output Illegal Instruction: 4 . Some searching online led me to this page:

https://apple.stackexchange.com/questions/387935/python-quit-working-and-now-i-get-illegal-instruction-4-message

I decided to try and downgrade numpy as well: conda install -n py3 numpy=1.17

I decided to test a file named test1.py using python via Terminal containing the following script:

num = int(input("Enter a number: "))
import numpy as np
print(num)
import sys
print(sys.version)

which I copied down based on the answer in the previous link. This ran just fine. Running with:

python -u -m trace -t test1.py

however in the terminal gave a ridiculously long stream of information.

Downgrading numpy to 1.18.5 solved the problem, at least for now.

The jupyter notebook github issues suggest uninstalling jupyter and reinstalling: https://github.com/jupyter/notebook/issues/1892#issuecomment-260403964

Regarding using multiple conda environments with jupyter, here is what I typically do:

  1. Install nb_conda_kernels in the base environment
    • conda install -n base nb_conda_kernels
  2. Install ipykernel in any conda environment that should be accessible in jupyter
    • conda install -n ENVNAME ipykernel

When using conda environments, it's best to use conda install when possible. statsmodels is available in conda , so you should conda install it.

the github page says to uninstall ipykernel and then re-install it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM