在定义函数或者类的.ipyter文件的末尾加上

try:
!jupyter nbconvert --to python ppp.ipynb #ppp是文件的名称
except:
pass

在需要导入到文件中:

import sys
#import os
#os.getcwd()
sys.path.append("G:/Jupyter") #ppp文件的路径 from ppp import dd

方法二:

首先创建一个model.py文件,放在一个固定的位置

import io, os,sys,types
from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import InteractiveShell class NotebookFinder(object):
"""Module finder that locates Jupyter Notebooks"""
def __init__(self):
self.loaders = {}
def find_module(self, fullname, path=None):
nb_path = find_notebook(fullname, path)
if not nb_path:
return
key = path
if path:
#lists aren't hashable
key = os.path.sep.join(path)
if key not in self.loaders:
self.loaders[key] = NotebookLoader(path)
return self.loaders[key] def find_notebook(fullname, path=None):
"""find a notebook, given its fully qualified name and an optional path
This turns "foo.bar" into "foo/bar.ipynb"
and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar
does not exist.
"""
name = fullname.rsplit('.', 1)[-1]
if not path:
path = ['']
for d in path:
nb_path = os.path.join(d, name + ".ipynb")
if os.path.isfile(nb_path):
return nb_path
# let import Notebook_Name find "Notebook Name.ipynb"
nb_path = nb_path.replace("_", " ")
if os.path.isfile(nb_path):
return nb_path
class NotebookLoader(object):
"""Module Loader for Jupyter Notebooks"""
def __init__(self, path=None):
self.shell = InteractiveShell.instance()
self.path = path
def load_module(self, fullname):
"""import a notebook as a module"""
path = find_notebook(fullname, self.path)
print ("importing Jupyter notebook from %s" % path)
# load the notebook object
with io.open(path, 'r', encoding='utf-8') as f:
nb = read(f, 4)
# create the module and add it to sys.modules
# if name in sys.modules:
# return sys.modules[name]
mod = types.ModuleType(fullname)
mod.__file__ = path
mod.__loader__ = self
mod.__dict__['get_ipython'] = get_ipython
sys.modules[fullname] = mod
# extra work to ensure that magics that would affect the user_ns
# actually affect the notebook module's ns
save_user_ns = self.shell.user_ns
self.shell.user_ns = mod.__dict__ try:
for cell in nb.cells:
if cell.cell_type == 'code':
# transform the input to executable Python
code = self.shell.input_transformer_manager.transform_cell(cell.source)
# run the code in themodule
exec(code, mod.__dict__)
finally:
self.shell.user_ns = save_user_ns
return mod
sys.meta_path.append(NotebookFinder())

然后创建被调用的func.ipynb文件

def foo():
print("")

最后在调用func.ipynb的文件中加入二者的路径

再导入这两个文件,注意更改func.ipynb后要重启kernel

将.ipynb文件导入到另外的文件中的更多相关文章

  1. [转载]将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,解决办法

    eclipse 代码中文注释乱码 求解决 将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclips ...

  2. PyCharm在同一个包(package)下,如何把一个.py文件导入另外一个.py文件下

    PyCharm在同一个包(package)下,如何把一个.py文件导入另外一个.py文件下 在同一个包下只需要用import 掉以后就可以找到模块所在的位置,但是如果不在同一个包下,在需要返回父级调用 ...

  3. SQL Server:把CSV文件导入到SQL Server表中

    有时候我们可能会把CSV中的数据导入到某个数据库的表中,比如做报表分析的时候. 对于这个问题,我想一点也难不倒程序人员吧!但是要是SQL Server能够完成这个任务,岂不是更好! 对,SQL Ser ...

  4. 把CSV文件导入到SQL Server表中

    保存数据库数据直接查询select * from tableName 在数据表格的左上角右击——将结果另存为选择路劲保存好的就是.csv格式的数据 有时候我们可能会把CSV中的数据导入到某个数据库的表 ...

  5. 如何将CAD文件导入到Protel和PADS中

    一. 如何把CAD中的板框图纸导入到Protel中? a. 在CAD中单位设置为“毫米”,并做简单的处理,板框图是有合并还是分解都无所谓,另存为R12(*dxf)格式文件. b. 打开DXP,新建PC ...

  6. 6.把建模工具导出的dea文件导入到three.js程序中

    1.使用Three.js渲染导出的DAE 在Three.js中使用Collada(即.dae)文件的话,首先得要用到 ColladaLoader.js. 但是这个ColladaLoader.js并不包 ...

  7. 如何将.sql文件导入到mysql的数据库中

    首先通过cmd的net start mysql57 启动mysql的服务器 然后,输入命令:mysql -h 127.0.0.1 -u root -p来启动mysql服务 最后 上图画红圈的部分是.s ...

  8. CSV文件导入到SQL Server表中

    USE 数据库名BULK Insert dbo.表名From 'c:\CSV文件名.csv'  WITH (   FIELDTERMINATOR = ',',   ROWTERMINATOR = '\ ...

  9. iOS-Pods里三方文件导入找不到头文件

    解决办法: 在target 里面 选择 build setting 搜索User Header Search Paths 然后 里面添加一条 ${SRCROOT}     recursive//后面填 ...

随机推荐

  1. 关闭eth0或者某个网络接口

    ifdown eth0 关闭eth0网络接口 ifup eth0 打开eth0网络接口 如果上面的命令无效,请使用下面的 ifconfig eth0 down 关闭eth0接口 ifconfig et ...

  2. 在公司上wifi

    公司的wifi上不了咋办?自己搞! 做法: 把自己的newifi mini带到公司, 记录公司内网ip,登入路由器设置, 1. 把ip改为内网一致, 2.关闭dhcp功能 3.设置wifi 网线连接: ...

  3. 混合pyqt和qtcreator (2): Impl a image viewer (can show FIji ROI manager data)

    # -*- coding: utf-8 -*- """ Simple demonstration of TreeWidget, which is an extension ...

  4. How to Export to Excel

    https://simpleisbetterthancomplex.com/tutorial/2016/07/29/how-to-export-to-excel.html Export data to ...

  5. 在linux环境下,php语法出错,怎样让php编译后提示编译错误,错误在哪?

    如果不具备修改php.ini的权限,可以如下:ini_set("display_errors", "On"); error_reporting(E_ALL |  ...

  6. Jenkins进阶-Git多仓库代码下载编译 (13)

    多仓库代码部署这个问题其实在13年做配置管理的时候,就遇到过这样的问题,而最近公司可视化图项目也需要多个仓库代码需要同时下载进行编译,由于仓库之间的代码编译有依赖关系,所以必须同时下载下来按照顺序进行 ...

  7. UML图类,接口之间的关系

    UML图类之间的关系(来自大话设计模式中的一张图,代表了类,接口之间的各种关系)

  8. npm是什么

    npm简介 维基百科中npm定义 npm完全用JavaScript写成,最初由艾萨克·施吕特(Isaac Z. Schlueter)开发. 艾萨克表示自己意识到"模块管理很糟糕"的 ...

  9. vs2015重新安装后,项目属性中的目标框架中没有framework4.6.1

    vs2015重新安装后,安装完后 项目属性中的目标框架中没有framework4.6.1,  控制面板的程序和功能中存在该安装包. 原因: NDP461-DevPack-KB3105179-CHS.e ...

  10. Spring Cloud(Dalston.SR5)--Hystrix 监控

    在服务调用者加入 Actuator ,可以对服务调用者的健康情况进行实时监控,例如,断路器是否打开.当前负载情况等. 服务调用者 需要增加 actuator依赖, 修改 POM.xml 中增加以下依赖 ...