安装 PyPy 和 IPyKernel 时出现问题

Problems installing PyPy and IPyKernel

提问人:docf 提问时间:7/1/2023 更新时间:7/3/2023 访问量:125

问:

所以首先介绍一下背景......

我正在尝试设置一个 JupyterLab 环境,在其中我可以(合理无缝地)在 Python 内核之间切换以评估代码性能。目标内核将是标准的 CPython(Python 3 - 主要是最新版本)和 PyPy。我正在使用 conda 来管理我的环境。

我已经设置了一个 JupyterLab 环境(在基本 conda 环境中安装 JupyterLab),通过基于不同的 CPython 版本创建各种 conda 环境(我也安装了 ipykernel),我可以轻松地在 JupyterLab 中更改运行时内核。目前为止,一切都好。

因此,要完成这项工作,我只需要在 conda 中创建一个 PyPy 环境(按照 PyPy 说明)并安装在其中。这似乎是不可能的。ipykernel

我的期望是按照 PyPy 指令创建一个 PyPy 环境,将 ipykernel 添加到其中,我将能够在 JupyterLab 中切换到基于 PyPy 的内核(就像我在 CPython 环境中成功完成的那样)。我的期望没有得到满足!

我首先在 conda 中创建一个 PyPy 环境:

conda config --set channel_priority strict
conda create -c conda-forge -n pypy-env pypy python=3.8

这需要一段时间,但似乎有效:

(base) PS C:\Windows\system32> conda env list
# conda environments:
#
base                  *  C:\tools\miniconda3
py-311                   C:\tools\miniconda3\envs\py-311
pypy-env                 C:\tools\miniconda3\envs\pypy-env

(base) PS C:\Windows\system32> conda activate pypy-env
(pypy-env) PS C:\Windows\system32> python --version
Python 3.8.16 | packaged by conda-forge | (a9dbdca6, May 11 2023, 16:56:34)
[PyPy 7.3.11 with MSC v.1929 64 bit (AMD64)]

尝试添加 ipykernel(在 JupyterLab 中启用内核切换所必需的)在经过相当激烈的尝试后失败,并突出显示了许多冲突(为方便起见而缩写):

(pypy-env) PS C:\Windows\system32> conda install ipykernel
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: -
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
failed

UnsatisfiableError: The following specifications were found to be incompatible with each other:

Output in format: Requested package -> Available versions

Package ca-certificates conflicts for:
pip -> python[version='>=2.7,<2.8.0a0'] -> ca-certificates
python=3.8 -> openssl[version='>=3.0.8,<4.0a0'] -> ca-certificates
...
...
Package six conflicts for:
ipykernel -> packaging -> six
pip -> html5lib -> six[version='>=1.9']
Note that strict channel priority may have removed packages required for satisfiability.

尽管 PyPy 建议,但通道优先级似乎导致了问题。将其从默认值设置回默认值strictstrictflexible

(pypy-env) PS C:\Windows\system32> conda config --set channel_priority flexible
(pypy-env) PS C:\Windows\system32>

实现看似令人满意的安装:

(pypy-env) PS C:\Windows\system32> conda install ipykernel
Collecting package metadata (current_repodata.json): done
Solving environment: done
...
...
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Retrieving notices: ...working... done

但是,启动 JupyterLab (JL) 并选择新的 pypy-env 内核会失败,因为 JL 日志确认:

CONDA_PREFIX=C:\tools\miniconda3
C:\tools\miniconda3\envs\pypy-env\lib\site-packages\jupyter_client\__init__.py:23: UserWarning: Could not import submodules
  warnings.warn("Could not import submodules")
Traceback (most recent call last):
  File "C:\tools\miniconda3\envs\pypy-env\Lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\tools\miniconda3\envs\pypy-env\Lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\tools\miniconda3\envs\pypy-env\lib\site-packages\ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "C:\tools\miniconda3\envs\pypy-env\lib\site-packages\ipykernel\__init__.py", line 5, in <module>
    from .connect import *  # noqa
  File "C:\tools\miniconda3\envs\pypy-env\lib\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' (C:\tools\miniconda3\envs\pypy-env\lib\site-packages\jupyter_client\__init__.py)

将 JL 内核更改为 CPython conda 环境(除了 JL 日志中的一些警告)似乎工作正常(并在笔记本中正确执行代码):

CONDA_PREFIX=C:\tools\miniconda3
This version of python seems to be incorrectly compiled
(internal generated filenames are not absolute).
This may make the debugger miss breakpoints.
Related bug: http://bugs.python.org/issue1666807
[I 2023-06-30 18:35:24.069 ServerApp] Connecting to kernel 834d68cd-6153-4f99-9beb-ded2e414bc14.
[I 2023-06-30 18:35:24.088 ServerApp] Connecting to kernel 834d68cd-6153-4f99-9beb-ded2e414bc14.
[I 2023-06-30 18:35:24.111 ServerApp] Connecting to kernel 834d68cd-6153-4f99-9beb-ded2e414bc14.

enter image description here

所以,综上所述,遵循 PyPy 的 conda 通道优先级建议,就无法安装 .忽略 PyPy 关于 conda 通道优先级的建议并坚持使用默认值,可以实现看似成功的安装,但实际上无法操作(至少在 和 JupyterLab 的上下文中)。strictipykernelflexibleipykernel

有人有任何建议如何纠正这种情况并启用与 ipykernel 和 JupyterLab 配合使用的 PyPy 的功能安装?

conda jupyter-lab pypy conda-forge

评论

0赞 mattip 7/2/2023
你的 condarc 中是否列出了 conda-forge 之前列出的默认通道?那么使用 will prefer default,它不支持 pypy。您应该对 PyPy 包使用 conda-forge 通道。strict
0赞 mattip 7/2/2023
你能列出你的包吗,并确保它们来自conda-forge?conda list
0赞 docf 7/3/2023
马蒂普...第一。。。。感谢您抽出宝贵时间回复!我检查了我的 .condarc 文件,它非常稀疏......只是条目“channel_priority:灵活” - 这是我从命令行设置的结果。因此,毫无疑问,基本默认值适用于 conda.io/projects/conda/en/latest/configuration.html
0赞 docf 7/3/2023
我检查了 pypy 环境中的 env 列表,很明显 conda-forge 包没有统一性。所以我会解释我做了什么,但它需要的不仅仅是这个评论字段给我的 400 个左右的字符!

答:

0赞 docf 7/3/2023 #1

好的,根据 mattip 的建议,我检查了 pypy 环境中已安装的软件包列表,并非所有软件包都来自 .因此,我删除了环境并重新开始。在构建新环境之前,我检查了我的 .condarc 文件内容:conda-forge

channel_priority: flexible

conda 通过以下方式使用通道 URL:conda info

    channel URLs : 
    https://repo.anaconda.com/pkgs/main/win-64                    
    https://repo.anaconda.com/pkgs/main/noarch                  
    https://repo.anaconda.com/pkgs/r/win-64                 
    https://repo.anaconda.com/pkgs/r/noarch                     
    https://repo.anaconda.com/pkgs/msys2/win-64                     
    https://repo.anaconda.com/pkgs/msys2/noarch

和 via :default channelsconda config --show

default_channels:
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - https://repo.anaconda.com/pkgs/msys2

这一次,我确保在环境创建的每个阶段都强制使用 conda-forge 通道:

(base) PS C:\Windows\system32> conda create -c conda-forge  --name pypy-env pypy python=3.8
(base) PS C:\Windows\system32> conda activate pypy-env
(pypy-env) PS C:\Windows\system32> conda install -c conda-forge ipykernel

初始环境创建命令安装独占来源的软件包。但是,当我安装时,jupyter_core有一个例外:conda-forgeipykernel

jupyter_client     conda-forge/noarch::jupyter_client-8.3.0-pyhd8ed1ab_0
jupyter_core       pkgs/main/win-64::jupyter_core-5.3.0-py38haa95532_0
libsodium          conda-forge/win-64::libsodium-1.0.18-h8d14728_1

尽管如此,这种方法还是奏效了。随后我运行了 jupyterlab,并且可以成功选择我的 pypy-env 环境并在其中运行代码。

Screenshot of JupyterLab

成功!

这个练习产生了一些进一步的问题,但这些问题可以针对单独的帖子