在Google Colab中导入一个本地模块或.py文件
2024-08-30 00:18:54
模块与单个.py文件的区别,模块中含有__init__.py文件,其中函数调用使用的是相对路径,如果使用导入.py文件的方法在Google Colab中导入模块
会报错:Attempted relative import in non-package
在Google Colab导入中导入一个本地模块
例,将pykalman模块导入,github地址 https://github.com/pykalman/pykalman
1.递归创建目录
!mkdir -p pykalman-master/pykalman
2.保存cell的内容到一个外部文件
%%writefile pykalman-master/pykalman/__init__.py
将__init__.py的内容复制进cell中
同理
%%writefile pykalman-master/pykalman/standard.py ······ %%writefile pykalman-master/pykalman/unscented.py ······ %%writefile pykalman-master/pykalman/utils.py ······
3.将pykalman-master目录添加到路径集合中
import sys
sys.path.append('pykalman-master')
4.导入新模块并调用其中的函数
from pykalman import KalmanFilter
在Google Colab导入中导入一个.py文件
例,定义example.py调用其中函数f():显示"Hello Python"
1.保存cell的内容到一个外部文件
%%writefile example.py
def f():
print 'Hello Python.'
2.将文件带入本地Python环境
execfile('example.py') f()
最新文章
- 连接池的实现 redis例子
- store前台数据过滤
- rowcount和@@Rowcount的区别,获取insert、update、delete影响行数
- 如何查询MySql日志
- Mysql-学习笔记(==》连接查询_高级查询五)
- 206. Reverse Linked List
- atprogram.exe : Atmel Studio Command Line Interface
- qtp不识别树结构中的点击事件
- ASP.NET MVC 第六回 过滤器Filter
- Configuring the JA-SIG CAS Client --官方
- MongoDB(三)mongoDB下载和安装
- 连载:面向对象的葵花宝典:思维、技能与实践(40) - DECORATOR模式
- HDU 2073 无限的路
- CentOS安装glibc-2.14,错误安装libc.so.6丢失急救办法
- Can't update: no tracked branch No tracked branch configured for branch dev.
- instanceof的用法
- Makefile中的%标记和系统通配符*的区别
- Delphi2010分 AnsiChar(1个字节) 和WideChar(2个字节) 。D7都是AnsiChar。
- 使用java调用fastDFS客户端进行静态资源文件上传
- odoo创建编号