ipykernel icon indicating copy to clipboard operation
ipykernel copied to clipboard

IPyKernel throws cannot import name write_connection_file

Open tungalbert99 opened this issue 2 years ago • 6 comments

Name: ipykernel Version: 6.15.1

On Apple M1 Pro Mac. OS: Monterey 12.3

On Python 3.9.13 with the following versions, IPyKernel fails "python -m ipykernel install --user --name=myenv"

jupyter               1.0.0
jupyter-client        7.3.4
jupyter-console       6.4.4
jupyter-core          4.11.1
jupyterlab-pygments   0.2.2
jupyterlab-widgets    1.1.1

Stack Trace:

[REMOVED]lib/python3.9/site-packages/jupyter_client/__init__.py:23: UserWarning: Could not import submodules
  warnings.warn("Could not import submodules")
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/opt/homebrew/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 147, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/opt/homebrew/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "[REMOVED]lib/python3.9/site-packages/ipykernel/__init__.py", line 5, in <module>
    from .connect import *  # noqa
  File "[REMOVED]/lib/python3.9/site-packages/ipykernel/connect.py", line 12, in <module>
    from jupyter_client import write_connection_file
ImportError: cannot import name 'write_connection_file' from 'jupyter_client' ([REMOVED]/lib/python3.9/site-packages/jupyter_client/__init__.py)

tungalbert99 avatar Jul 12 '22 22:07 tungalbert99

Hi @tungalbert99, I'm not sure why it is hitting the warnings.warn("Could not import submodules") condition here in your setup. Can you please try editing lib/python3.9/site-packages/jupyter_client/__init__.py to call raise after the warning and give the traceback?

blink1073 avatar Jul 13 '22 01:07 blink1073

Also just started running into this, and am wondering if it's due to a different issue with _ctypes (not if the same in the above case though).

I am using Ubuntu (via WSL2) and Python 3 (via pyenv). I ran into an issue with _ctypes import previously and believe it was resolved by pip install -U setuptools, as setuptools was not recognized previously (even though allegedly installed when trying to install it again).

❯ python -m ipykernel install --user --name jupyter_test --display-name "Python (jupyter_test)"
[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/jupyter_client/__init__.py:23: UserWarning: Could not import submodules
  warnings.warn("Could not import submodules")
Traceback (most recent call last):
  File "[REMOVED]/.pyenv/versions/3.10.0/lib/python3.10/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "[REMOVED]/.pyenv/versions/3.10.0/lib/python3.10/runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "[REMOVED]/.pyenv/versions/3.10.0/lib/python3.10/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/ipykernel/__init__.py", line 2, in <module>
    from .connect import *
  File "[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/ipykernel/connect.py", line 10, in <module>
    import jupyter_client
  File "[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/jupyter_client/__init__.py", line 8, in <module>
    from .asynchronous import AsyncKernelClient  # noqa
  File "[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/jupyter_client/asynchronous/__init__.py", line 1, in <module>
    from .client import AsyncKernelClient  # noqa
  File "[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/jupyter_client/asynchronous/client.py", line 6, in <module>
    from jupyter_client.channels import HBChannel
  File "[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/jupyter_client/channels.py", line 12, in <module>
    import zmq.asyncio
  File "[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/zmq/__init__.py", line 61, in <module>
    _load_libzmq()
  File "[REMOVED]/.pyenv/versions/jupyter_test/lib/python3.10/site-packages/zmq/__init__.py", line 23, in _load_libzmq
    import ctypes
  File "[REMOVED]/.pyenv/versions/3.10.0/lib/python3.10/ctypes/__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'

UPDATE:

Removing libffi from brew (as recommended by this GitHub answer) seems to have resolved this specific issue, although I cannot run Jupyter Notebook yet, but that is most likely another issue.

❯ brew remove --ignore-dependencies libffi
Uninstalling /home/linuxbrew/.linuxbrew/Cellar/libffi/3.4.2... (18 files, 665.8KB)
❯ pyenv install 3.10.0
pyenv: /home/kendall/.pyenv/versions/3.10.0 already exists
continue with installation? (y/N) y

kendallroth avatar Jul 13 '22 18:07 kendallroth

Thanks for the additional context @kendallroth!

blink1073 avatar Jul 14 '22 14:07 blink1073

@tungalbert99 Hello! I am also running into this issue. Have you found the solution yet?

Sofiia2001 avatar Aug 16 '22 13:08 Sofiia2001

@Sofiia2001 I'm not entirely sure. I ended up re-creating a new environment and it started working again.

tungalbert99 avatar Aug 17 '22 22:08 tungalbert99

This happened to me on linux what I had to do was actually install libffi and then uninstall, reinstall pyenv installations from this stackoverflow answer https://stackoverflow.com/a/60374453/1649917

sudo apt-get install libffi-dev
pyenv uninstall 3.7.6
pyenv install 3.7.6

spookyuser avatar Sep 20 '22 13:09 spookyuser