解决 ipykernel 安装错误

130 阅读2分钟

该问题出现在用户尝试使用python3 -m pip install ipykernel命令为 Jupyter 安装 Python 3 内核时。在执行该命令后,用户遇到了异常:

Exception:
Traceback (most recent call last):
  File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 2482, in _dep_map
    return self.__dep_map
  File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 2344, in __getattr__
    raise AttributeError(attr)
AttributeError: _DistInfoDistribution__dep_map

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 278, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/usr/lib/python3/dist-packages/pip/req.py", line 1266, in prepare_files
    req_to_install.extras):
  File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 2291, in requires
    dm = self._dep_map
  File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 2484, in _dep_map
    self.__dep_map = self._compute_dependencies()
  File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 2508, in _compute_dependencies
    parsed = next(parse_requirements(distvers))
  File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 2605, in parse_requirements
    line, p, specs = scan_list(VERSION,LINE_END,line,p,(1,2),"version spec")
  File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 2583, in scan_list
    "Expected ',' or end-of-list in",line,"at",line[p:]
ValueError: ("Expected ',' or end-of-list in", 'pytest ==3.2.*', 'at', '*')

2. 解决方案

为了解决此问题,请尝试以下步骤:

  1. 确保您已安装 Python 3。

使用以下命令检查 Python 3 是否已安装:

python --version

此命令应该输出类似以下的内容:

Python 3.x.x
  1. 如果 Python 3 未安装,请安装它。

您可以通过以下方式安装 Python 3:

  • 从 Python 官网下载 Python 3 安装程序并运行它。
  • 使用您的软件包管理器安装 Python 3(例如,在 Ubuntu 上使用 apt install python3)。
  1. 安装 ipykernel。

使用以下命令安装 ipykernel:

pip install ipykernel

此命令应该成功安装 ipykernel。

  1. 将 Python 3 内核添加到 Jupyter。

使用以下命令将 Python 3 内核添加到 Jupyter:

ipython kernel install --user --name=python3

此命令应该成功将 Python 3 内核添加到 Jupyter。

  1. 检查 Jupyter 中是否可用 Python 3 内核。

使用以下命令检查 Jupyter 中是否可用 Python 3 内核:

jupyter kernelspec list

此命令应该输出类似以下的内容:

Available kernels:
  python2    /usr/local/share/jupyter/kernels/python2
  python3    /usr/local/share/jupyter/kernels/python3

现在,您应该能够在 Jupyter 中使用 Python 3 内核了。