如何在Anaconda中安装SageMath内核? [英] How to install SageMath kernel in Anaconda?

查看:399
本文介绍了如何在Anaconda中安装SageMath内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Anaconda 3中使用Sage,但看起来库未导入.

I'm trying to use Sage in Anaconda 3 but it looks that the libraries are not imported.

我首先创建了一个新环境'ipykernel_py2',然后按照

I firstly created a new environment 'ipykernel_py2' and then installed Python 2 as explained in here. With this I can have both Python 3 and Python 3 up and running in Anaconda 3.

然后我转到创建的内核文件夹(C:\ Users \ YOUR_USERNAME \ AppData \ Local \ Continuum \ anaconda3 \ envs \ ipykernel_py2 \ share \ jupyter \ kernels)并粘贴Sage的内核(取自C:\ Program Files \ SageMath 8.2 \运行时\ opt \ sagemath-8.2 \ local \ share \ jupyter \ kernels).这允许在Jupyter中创建新的SageMath文件,但内核已死. 要激活内核,我使用了Anaconda Prompt并输入:

Then I went to the kernel's folder created (C:\Users\YOUR_USERNAME\AppData\Local\Continuum\anaconda3\envs\ipykernel_py2\share\jupyter\kernels) and pasted Sage's kernel (taken from C:\Program Files\SageMath 8.2\runtime\opt\sagemath-8.2\local\share\jupyter\kernels). This allows to create new SageMath files in Jupyter but the kernel is dead. To activate the kernel I used Anaconda Prompt and typed:

activate ipykernel_py2
python -m ipykernel install --user --name sagemath --display-name "SageMath 8.2"

因此,内核现已激活,我可以创建和运行Sage文件.但是,这些库仍无法正常工作.该文件似乎像普通的Python 2文件一样​​运行.

So the kernel is now activated and I can create and run Sage files. However the libraries are still not working. It seems that the file is running like a normal Python 2 file.

有人知道如何解决此问题吗?我需要创建一个单独的环境吗?

Does anyone know how to fix this? Do I need to create a seperate environment?

推荐答案

Sage for Windows在称为Cygwin的UNIX仿真环境下运行.查看它包含的sagemath/kernel.json:

Sage for Windows runs under a UNIX emulation environment called Cygwin. Looking at the sagemath/kernel.json it contains:

{"display_name": "SageMath 8.2", "argv": ["/opt/sagemath-8.2/local/bin/sage", "--python", "-m", "sage.repl.ipython_kernel", "-f", "{connection_file}"]}

您可以在此处看到它具有sage可执行文件的UNIX样式的路径.此路径仅对在Sage的Cygwin环境下运行的其他程序有意义,对本机Windows程序没有意义.简单地将其转换为等效的Windows路径也不起作用,因为bin/sage实际上是一个shell脚本.至少需要为Cygwin随附的bash提供 Windows 路径,并将 UNIX 路径传递给sage可执行文件(与上面的一个).如果没有登录外壳,则大多数环境变量也不会设置,因此您可能需要bash -l.

You can see here that it has a UNIX-style path to the sage executable. This path only makes sense to other programs running under Sage's Cygwin environment, and is meaningless to native Windows programs. Simply converting it to the equivalent Windows path won't work either, because bin/sage is actually a shell script. At the very least you need to provide a Windows path to the bash that comes with Cygwin and pass it the UNIX path to the sage executable (the same as the one above). Without a login shell, most environment variables needed won't be set either, so you probably need bash -l.

所以,像这样:

{"display_name": "SageMath 8.2", "argv": ["C:\\Program Files\\SageMath 8.2\\runtime\\bin\\bash.exe", "-l", "/opt/sagemath-8.2/local/bin/sage", "--python", "-m", "sage.repl.ipython_kernel", "-f", "{connection_file}"]}

可能起作用.我不确定的一件事是{connection_file}参数是否也将被正确处理.我还没有测试.

might work. The one thing I'm not sure about is whether the {connection_file} argument will be handled properly either. I haven't tested it.

更新:确实,以上内容部分起作用,但是存在一些问题:{connection_file}参数作为Windows的绝对路径传递给该文件.虽然Cygwin通常可以从Windows路径透明地转换为相应的UNIX路径,但是存在已知问题 Cygwin上Python的os.path模块不能很好地处理Windows样式的路径,这会导致问题.

Update: Indeed, the above partially works, but there are a few problems: The {connection_file} argument as passed as the absolute Windows path to the file. While Cygwin can normally translate transparently from Windows paths to a corresponding UNIX path, there is a known issue that Python's os.path module on Cygwin does not handle Windows-style paths well, and this leads to issues.

我遇到的另一个主要问题是 IPKernelApp 具有一个线程,该线程进行轮询以查看内核的父进程(在本例中为笔记本服务器)是否已退出,因此如果父进程关闭,则可以适当地将其关闭.这是内核知道在杀死笔记本服务器时自动关闭的方式.

The other major problem I encountered was that IPKernelApp, the class that drives generic Jupyter kernels, has a thread which polls to see if the kernel's parent process (in this case the notebook server) has exited, so it can appropriately shut down if the parent shuts down. This is how kernels know to automatically shut down when you kill the notebook server.

如何完成此操作因平台而异-Windows与类似UNIX的平台不同.由于Sage的内核在Cygwin中运行,因此它选择了类似UNIX的轮询器.但是,如果笔记本服务器恰好是本地Windows进程,则这是错误的,就像在Windows本地Jupyter中运行Sage内核时一样.值得注意的是, Windows父级轮询器 可以在Cygwin上正常工作,因为它可以通过ctypes访问Windows API.因此,可以通过为IPKernelApp提供一个包装程序来强制使用ParentPollerWindows来解决此问题.

How this is done is very different depending on the platform--Windows versus UNIX-like. Because Sage's kernel runs in Cygwin, it chooses the UNIX-like poller. However, this is wrong if the notebook server happens to be a native Windows process, as is the case when running the Sage kernel in a Windows-native Jupyter. Remarkably, the parent poller for Windows can work just as well on Cygwin since it accesses the Windows API through ctypes. Therefore, this can be worked around by providing a wrapper to IPKernelApp that forces uses of ParentPollerWindows.

然后可能的解决方案如下所示:从SageMath Shell中执行:

A possible solution then looks something like this: From within the SageMath Shell do:

$ cd ~
$ mkdir -p ./local/share/jupyter/kernels/sagemath
$ cd ./local/share/jupyter/kernels/sagemath
$ cat <<_EOF_ > kernel-wrapper.sh
#!/bin/sh
here="$(dirname "$0")"
connection_file="$(cygpath -u -a "$1")"
exec /opt/sagemath-8.2/local/bin/sage --python "${here}/kernel-wrapper.py" -f "${connection_file}"
_EOF_
$ cat <<_EOF_ > kernel-wrapper.py
from ipykernel.kernelapp import IPKernelApp as OrigIPKernelApp
from ipykernel.parentpoller import ParentPollerWindows
from sage.repl.ipython_kernel.kernel import SageKernel


class IPKernelApp(OrigIPKernelApp):
    """
    Although this kernel runs under Cygwin, its parent is a native Windows
    process, so we force use of the ParentPollerWindows.
    """

    def init_poller(self):
        if self.interrupt or self.parent_handle:
            self.poller = ParentPollerWindows(self.interrupt,
                                              self.parent_handle)


IPKernelApp.launch_instance(kernel_class=SageKernel)
_EOF_

现在,将kernel.json(在share\jupyter\kernels\sagemath下的现有位置)编辑为:

Now edit the kernel.json (in its existing location under share\jupyter\kernels\sagemath) to read:

{"display_name": "SageMath 8.2", "argv": ["C:\\Program Files\\SageMath 8.2\\runtime\\bin\\bash.exe", "-l", ".local/share/jupyter/kernels/sagemath/kernel-wrapper.sh", "{connection_file}"]}

这会运行kernel-wrapper.sh,而后者又会运行kernel-wrapper.py. (我可以做一些简化,以完全摆脱对kernel-wrapper.sh的需要,但是在SageMath 8.3中,它会更容易,其中包括

This runs kernel-wrapper.sh which in turn runs kernel-wrapper.py. (There are a few simplifications I could make to get rid of the need for kernel-wrapper.sh completely, but that would be easier in SageMath 8.3 which includes PyCygwin.)

正如您所看到的,这并非微不足道,而且从来都不是故意设计的.但这是可以完成的.一旦内核本身启动并运行,只需通过TCP/IP套接字与之对话即可,因此在此之后不会涉及太多魔术.我相信在Jupyter方面和Sage方面都可以进行一些小的改进,这些改进将来会促进这种事情的发生.

As you can see, this was not trivial, and was never by design meant to be possible. But it can be done. Once the kernel itself is up and running it's just a matter of talking to it over TCP/IP sockets so there's not too much magic involved after that. I believe there are some small improvements that could be made on both the Jupyter side and on the Sage side that would facilitate this sort of thing in the future...

这篇关于如何在Anaconda中安装SageMath内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆