如何在google Colaboratory中导入.py?

이성령*_*이성령 5 import google-colaboratory

我想简化代码.所以我做了一个utils.py,但Google Colaboratory目录是"/ content"我读了其他问题.但这不是我的解决方案

在Google的Colab笔记本中,如何从Python文件中调用函数?

%%writefile example.py
def f():
 print 'This is a function defined in a Python source file.'
# Bring the file into the local Python environment.
execfile('example.py')
f()
This is a function defined in a Python source file.
Run Code Online (Sandbox Code Playgroud)

看起来好像只使用def().
使用这个,我总是在单元格中编写代码.

但我想要这个代码

import example.py
example.f()
Run Code Online (Sandbox Code Playgroud)

小智 0

我最近也遇到了这个问题。

我通过以下步骤解决了这个问题,尽管这不是一个完美的解决方案。

src = list(files.upload().values())[0]
open('util.py','wb').write(src)
import util
Run Code Online (Sandbox Code Playgroud)