使用 Python 3.10 运行单元需要安装 ipykernel

Posted

技术标签:

【中文标题】使用 Python 3.10 运行单元需要安装 ipykernel【英文标题】:Running cells with Python 3.10 requires ipykernel installed 【发布时间】:2021-12-15 11:38:56 【问题描述】:

我刚刚在我的笔记本电脑(Ubuntu 20.04)上安装了 Python 3.10。

在 VS Code 中运行 Jupyter Notebook 适用于 Python 3.9,但不适用于 Python 3.10。我收到错误消息:Running cells with 'Python 3.10.0 64 bit' requires ipykernel installed or requires an update

我尝试过的事情:

点击重新安装,运行:
/usr/bin/python3.10 /home/joris/.vscode/extensions/ms-python.python-2021.10.1365161279/pythonFiles/shell_exec.py /usr/bin/python3.10 -m pip install -U --force-reinstall ipykernel /tmp/tmp-12568krFMIDJVy4jp.log
正在运行 pip3 install --upgrade ipykernel jupyter notebook pyzmq(来自 this 线程)。

编辑

按照 cmets 的要求,这是我单击“重新安装”按钮时的输出:
/usr/bin/python3.10 /home/joris/.vscode/extensions/ms-python.python-2021.10.1365161279/pythonFiles/shell_exec.py /usr/bin/python3.10 -m pip install -U --force-reinstall ipykernel /tmp/tmp-10997AnLZP3B079oV.log
Executing command in shell >> /usr/bin/python3.10 -m pip install -U --force-reinstall ipykernel
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3/dist-packages/pip/__main__.py", line 19, in <module>
    sys.exit(_main())
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/main.py", line 73, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "/usr/lib/python3/dist-packages/pip/_internal/commands/__init__.py", line 96, in create_command
    module = importlib.import_module(module_path)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py", line 24, in <module>
    from pip._internal.cli.req_command import RequirementCommand
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py", line 15, in <module>
    from pip._internal.index.package_finder import PackageFinder
  File "/usr/lib/python3/dist-packages/pip/_internal/index/package_finder.py", line 21, in <module>
    from pip._internal.index.collector import parse_links
  File "/usr/lib/python3/dist-packages/pip/_internal/index/collector.py", line 12, in <module>
    from pip._vendor import html5lib, requests
ImportError: cannot import name 'html5lib' from 'pip._vendor' (/usr/lib/python3/dist-packages/pip/_vendor/__init__.py)
Traceback (most recent call last):
  File "/home/joris/.vscode/extensions/ms-python.python-2021.10.1365161279/pythonFiles/shell_exec.py", line 26, in <module>
    subprocess.check_call(shell_args, stdout=sys.stdout, stderr=sys.stderr)
  File "/usr/lib/python3.10/subprocess.py", line 369, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/bin/python3.10', '-m', 'pip', 'install', '-U', '--force-reinstall', 'ipykernel']' returned non-zero exit status 1.
这是我的_vendor 文件夹包含的内容:
joris@joris-N751JK:~$ ls /usr/lib/python3/dist-packages/pip/_vendor/
__init__.py  __pycache__
这是重新安装 pip 并检查 _vendor 文件的输出:
joris@joris-N751JK:~$ python3 -m pip install --upgrade --force-reinstall pip
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
  Using cached pip-21.3.1-py3-none-any.whl (1.7 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.3.1
    Uninstalling pip-21.3.1:
      Successfully uninstalled pip-21.3.1
Successfully installed pip-21.3.1
joris@joris-N751JK:~$ ls /usr/lib/python3/dist-packages/pip/_vendor
__init__.py  __pycache__

【问题讨论】:

重新安装 ipykernel 的输出是什么?错误信息是什么? @Steven-MSFT,请看我的编辑 你能在_vendor文件夹中找到html5lib的包吗?你的pip好像有问题,你可以试试重新安装吗? 我添加了我的_vendor -文件夹的内容 你的pip有问题,需要重新安装你的pip。你可以看看你已经安装的python3.9。 【参考方案1】:

我认为 ipykernel 不兼容 3.10。

以下是我尝试使用以下命令安装 ipykernel 时收到的消息:conda install -c anaconda ipykernel

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - ipykernel -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.8,<3.9.0a0|>=3.5,<3.6.0a0|>=3.9,<3.10.0a0']

Your python: python=3.10

解决方案:

我建议使用conda 或另一个library 创建virtual environment。

让您开始:

    安装Anaconda。 输入以下命令。 [注意:这些是 Windows 命令 - 在 Mac 和 Linux 上可能略有不同。]
# Create virtual environment
# Use a version of Python that is less than 3.10
conda create --name your_env_name python<3.10

# Activate new environment
conda activate your_env_name

# Install ipykernel
conda install -c anaconda ipykernel

# Add this new environment to your Jupyter Notebook kernel list
ipython kernel install --name your_env_name --user

# Windows only: When trying to launch `jupyter notebook`, you may receive a win32api error.
# The command below fixes that issue.
conda install -c anaconda pywin32

【讨论】:

谢谢。有人支持吗?欢迎任何未来的更新 我会接受这个答案,直到给出更自信的答案

以上是关于使用 Python 3.10 运行单元需要安装 ipykernel的主要内容,如果未能解决你的问题,请参考以下文章

Python 3.10 不支持 pygame 吗?

安装相似性搜索库 nmslib windows 10 Python 3.10

Python 3.10 的 Matplotlib 安装问题

在 Windows10 上的 Python 3.10 中安装 scipy 时出错

在 Python 3.10 上启动 Spyder IDE 时出错

无法在 Pycharm 上使用 Python 3.10 作为解释器