Python 如何在pip安装的jupyter中定义自定义魔法

Python 如何在pip安装的jupyter中定义自定义魔法,python,jupyter,ipython-magic,jupyter-notebook,Python,Jupyter,Ipython Magic,Jupyter Notebook,这是这个的延伸 我想在通过pip安装SAS内核时安装SAS Magic。 如果我从sas_kernel导入包,它将注册。magics import sas_magic 但是我希望它不需要导入就可以使用。 我使用的是jupyter 4.0.6 以下是代码片段: from __future__ import print_function import IPython.core.magic as ipym from saspy.SASLogLexer import * import re import

这是这个的延伸 我想在通过pip安装SAS内核时安装SAS Magic。 如果我从sas_kernel导入包
,它将注册。magics import sas_magic

但是我希望它不需要导入就可以使用。 我使用的是jupyter 4.0.6

以下是代码片段:

from __future__ import print_function
import IPython.core.magic as ipym
from saspy.SASLogLexer import *
import re
import os

@ipym.magics_class
class SASMagic(ipym.Magics):

    @ipym.cell_magic
    def SAS(self,line,cell):
        '''
        %%SAS - send the code in the cell to a SAS Server
        '''
        executable = os.environ.get('SAS_EXECUTABLE', 'sas')
        if executable=='sas':
            executable='/usr/local/SASHome/SASFoundation/9.4/sas'
        e2=executable.split('/')
        _path='/'.join(e2[0:e2.index('SASHome')+1])
        _version=e2[e2.index('SASFoundation')+1]
        import saspy as saspy
        self.mva=saspy.SAS_session()
        self.mva._startsas(path=_path, version=_version)

        res=self.mva.submit(cell,'html')
        output=self._clean_output(res['LST'])
        log=self._clean_log(res['LOG'])
        dis=self._which_display(log,output)
        return dis

from IPython import get_ipython
get_ipython().register_magics(SASMagic)

由于内核是从元内核派生的,因此可以在
内核中注册magics

from .sasmagic import SASMagic

class SASKernel(MetaKernel):
    def __init__(self, **kwargs):
        super(SASKernel, self).__init__(**kwargs)
        self.register_magics(SASMagic)

(我假设有一点,因为我看不到您的代码,但它看起来像这样,假设您的SASMagic定义位于内核类定义旁边的
SASMagic.py

sas_内核是IPython内核的派生吗?如果是,在子类中,您可以将其添加到内核的初始化中。@minrk sas_内核是元内核的派生,它是内核的派生。所以你是说我可以将它添加到sas_内核的init方法中,并在运行
pip install sas_kernel
后进行注册?很抱歉,有个基本问题,sas_内核中的代码是什么样子的?