Python 如何在 google colab 中导入自定义模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52733786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:10:23  来源:igfitidea点击:

How to import custom modules in google colab?

pythongoogle-colaboratory

提问by Shreyas Ramachandran

I have a file named imutils.py that has just one definition namely abc() which returns the sum of 2 integers.

我有一个名为 imutils.py 的文件,它只有一个定义,即 abc(),它返回 2 个整数的总和。

Now I want to use this definition in a separate collab file but I am unable to.

现在我想在一个单独的协作文件中使用这个定义,但我不能。

The method I used was to first upload the file imutils.py to drive and then importing it and using the definition. The error says module 'imutils' has no attribute 'abc'

我使用的方法是首先将文件 imutils.py 上传到驱动器,然后将其导入并使用定义。错误说模块“imutils”没有属性“abc”

To upload I first used 2 methods : First I uploaded using the drive GUI and then I also tried the above using the code. Uploading in both cases was successful

上传我首先使用了两种方法:首先我使用驱动器 GUI 上传,然后我还使用代码尝试了上述方法。两种情况都上传成功

from google.colab import files
files.upload() 

回答by Bob Smith

If your Python file is in Drive, it's likely simpler to mount your Drive than to upload the file, e.g.,

如果您的 Python 文件在 Drive 中,则挂载 Drive 可能比上传文件更简单,例如,

from google.colab import drive
drive.mount('/content/gdrive')

Then, if you have a module, you can import it like so:

然后,如果你有一个模块,你可以像这样导入它:

https://colab.research.google.com/drive/1uvHuizCBqFgvbCwEhK7FvU8JW0AfxgJw

https://colab.research.google.com/drive/1uvHuizCBqFgvbCwEhK7FvU8JW0AfxgJw

Contents of the Notebook follow:

笔记本内容如下:

Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code

在浏览器中转至此 URL:https: //accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Awscope%3Aytf%3Aietf%3Aietf%3A email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth% 2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code

Enter your authorization code:

输入您的授权码:

··········

··········

Mounted at /content/gdrive

安装在 /content/gdrive

I happen to have an existing .pyfile in Drive.

我碰巧.py在云端硬盘中有一个现有文件。

!ls /content/gdrive/My\ Drive/*.py
>>> /content/gdrive/My Drive/mylib.py

!cat '/content/gdrive/My Drive/mylib.py'

def MyFunction():
    print ('My imported function')

# We'll need to update our path to import from Drive.

import sys
sys.path.append('/content/gdrive/My Drive')

# Now we can import the library and use the function.

import mylib
mylib.MyFunction()