在 Google Colab 中导入 .py 文件
Importing .py files in Google Colab
有没有什么方法可以将我的代码上传到 .py 文件中并将它们导入到 colab 代码单元格中?
我找到的另一种方法是在本地创建一个 Jupyter 笔记本,然后将其上传到 Colab,这是唯一的方法吗?
可以先保存,再导入。
from google.colab import files
src = list(files.upload().values())[0]
open('mylib.py','wb').write(src)
import mylib
更新(2018 年 11 月):现在您可以通过
轻松上传
- 单击 [>] 打开左窗格
- 选择文件选项卡
- 点击[上传]并选择您的[mylib.py]
- 导入 mylib
更新(2019年10月):如果不想每次都上传,可以先存到S3再挂载到Colab,如图this gist
更新(2020 年 4 月):现在您可以自动安装 Google 驱动器。从云端硬盘复制比上传更容易。
- 将
mylib.py
存储在您的云端硬盘中
- 打开一个新的 Colab
- 打开(左)侧窗格,select
Files
查看
- 点击
Mount Drive
然后 Connect to Google Drive
- 复制
!cp drive/MyDrive/mylib.py .
import mylib
试试这个方法:
我有一个名为 plant_seedlings 的包。该包存储在 google 驱动器中。我应该做的是将这个包复制到 /usr/local/lib/python3.6/dist-packages/.
!cp /content/drive/ai/plant_seedlings.tar.gz /usr/local/lib/python3.6/dist-packages/
!cd /usr/local/lib/python3.6/dist-packages/ && tar -xzf plant_seedlings.tar.gz
!cd /content
!python -m plant_seedlings
- 您可以使用 google.colab.filesupload() 函数将本地文件上传到 google colab
- 如果您在 github 上有文件,请使用
!git 克隆 https://github.com/username/repo_name.git。然后就像在 jupyter notebook 中一样使用魔术函数加载它 %load
%load filename.py
.
根据 Korakot Chaovavanich 的回答,我创建了以下函数来下载 Colab 实例中所需的所有文件。
from google.colab import files
def getLocalFiles():
_files = files.upload()
if len(_files) >0:
for k,v in _files.items():
open(k,'wb').write(v)
getLocalFiles()
然后您可以使用通常的 'import' 语句将您的本地文件导入 Colab。希望对您有所帮助
您可以将这些 .py 文件上传到 Google 驱动器并允许 Colab 使用它们:
!mkdir -p drive
!google-drive-ocamlfuse drive
根文件夹中的所有文件和文件夹都将位于 drive
。
现在是 2019 年 6 月。
确保在 Python 包的 __init__.py
中所有相关文件都按顺序导入。将代码推送到 Git 或使用 .
例如,
from .Boxes import *
from .Circles import *
from .Rectangles import *
...
不要使用 __init__.py
文件中的包名称来导入文件。
在 Google colab 中,
! rm -rf SorghumHeadDetection
! git clone https://github.com/user/amazing-repo-name/
我遇到了同样的问题。在阅读了无数post之后,我想介绍以下我最终选择的解决方案(例如使用urllib
、httpimport
、从[=59=克隆]、打包安装模块等)。该解决方案利用 Google 驱动器 API (official doc) 进行适当的授权。
优点:
- 简单安全(无需代码处理文件操作异常and/or额外授权)
- 模块文件由 Google 帐户凭据保护(没有其他人可以 view/take/edit 它们)
- 您可以控制 upload/access 的内容(您可以 change/revoke 在逐个文件的基础上随时访问)
- 一切尽在一处(无需依赖或管理其他文件托管服务)
- 自由 rename/relocate 模块文件(不基于路径,不会破坏 your/other 的笔记本代码)
步骤:
- 将您的 .py 模块文件保存到 Google 驱动器 - 您应该拥有它,因为您已经在使用 Colab
- 右击"Get shareable link",复制“
id=
”后面的部分——GoogleDrive 分配的文件id
- 将以下代码片段添加并运行到您的 Colab 笔记本中:
!pip install pydrive # Package to use Google Drive API - not installed in Colab VM by default
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth # Other necessary packages
from oauth2client.client import GoogleCredentials
auth.authenticate_user() # Follow prompt in the authorization process
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
your_module = drive.CreateFile({"id": "your_module_file_id"}) # "your_module_file_id" is the part after "id=" in the shareable link
your_module.GetContentFile("your_module_file_name.py") # Save the .py module file to Colab VM
import your_module_file_name # Ready to import. Don't include".py" part, of course :)
旁注
最后但同样重要的是,我应该相信这种方法的 original contributor。 post 可能在代码中有一些拼写错误,因为它在我尝试时触发了错误。经过更多阅读和故障排除后,我上面的代码片段有效(截至今天 Colab VM OS:Linux 4.14.79)。
以防其他人有兴趣知道如何从 google colab 中的 gdrive 导入 files/packages。以下程序对我有用:
1) 在 google colab 中安装您的 google 驱动器:
from google.colab import drive
drive.mount('/content/gdrive/')
2) 使用 sys:
将目录附加到您的 python 路径
import sys
sys.path.append('/content/gdrive/mypythondirectory')
现在您应该可以从该目录导入内容了!
一个简单的方法是
- 输入
从 google.colab 导入文件
上传 = files.upload()
- 复制代码
- 粘贴到 colab 单元格中
我们可以做到。
import sys
import os
py_file_location = "/content/drive/My Drive"
sys.path.append(os.path.abspath(py_file_location))
现在您可以将其作为模块导入到该位置的笔记本中。
import whatever
以下是对我有用的步骤
在 google colab
中安装您的 google 驱动器
从 google.colab 导入驱动器
drive.mount('/content/drive')
插入目录
导入系统
sys.path.insert(0,'/content/drive/我的Drive/ColabNotebooks')
检查当前目录路径
%光驱/我的Drive/ColabNotebooks
%密码
导入您的模块或文件
进口my_module
如果出现以下错误'Name Null is not defined',请执行以下操作
5.1 从 colab 下载 my_module.ipynb 作为 my_module.py 文件(文件->下载 .py)
5.2 上传*.py文件到drive/MyDrive/ColabNotebooks in Google drive
5.3 导入 my_module 现在可以使用了
这是我的过程:
import sys
sys.path.insert(0, '/content/drive/MyDrive/my_folder')
%cd /content/drive/MyDrive/my_folder
%pwd
现在,您可以使用 import my_module
轻松地从该路径导入模块
您可以通过将驱动器挂载到 colab 并编写一些代码来放置 python 文件的 ID 来完成此操作
你可以在这里找到代码
importing python file from drive to colab
# Code to read file into colaboratory:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
#Autheticate E-Mail ID
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
#2.1 Get the file
your_module = drive.CreateFile({"id": 'write your file id here'}) # "your_module_file_id" is the part after "id=" in the shareable link
your_module.GetContentFile("write the file name here") # Save the .py module file to Colab VM
import file_name
from file_name import anything #as classes or functions from your file
os.listdir
可以用来查看目录下的所有文件
from google.colab import drive
drive.mount("/content/drive")
import os
path="/content/drive/My Drive/Colab Notebooks"
os.chdir(path)
os.listdir(path)`
就我而言,我尝试使用的文件名为 client.py
。这引起了冲突,因为 /usr/local/lib/python3.7/dist-packages/
.
中已经有一个名为 client
的库
我通过将 client.py
文件上传到保存 Colab Notebook 的同一 Google Drive 文件夹并将其名称更改为未出现在 dist-packages
文件夹。
在我的例子中,我将文件名更改为 dfsclient.py
然后用
导入它
import dfsclient
然后我实现了 Kamal 的答案:
import sys
sys.path.insert(0, '/content/drive/MyDrive/my_folder')
我经常这样做:
将我的模块保存在目录中。在 MyModules
中说 MyFile.py
定义我的模块的位置:
path_m = '/content/drive/MyDrive/Colab Notebooks/MyModules/'
然后我把路径加到sys.path:
import sys
sys.path.insert(0,path_m)
import the module into my Jupyter/Google Colab notebook.
import MyFile
有没有什么方法可以将我的代码上传到 .py 文件中并将它们导入到 colab 代码单元格中?
我找到的另一种方法是在本地创建一个 Jupyter 笔记本,然后将其上传到 Colab,这是唯一的方法吗?
可以先保存,再导入。
from google.colab import files
src = list(files.upload().values())[0]
open('mylib.py','wb').write(src)
import mylib
更新(2018 年 11 月):现在您可以通过
轻松上传- 单击 [>] 打开左窗格
- 选择文件选项卡
- 点击[上传]并选择您的[mylib.py]
- 导入 mylib
更新(2019年10月):如果不想每次都上传,可以先存到S3再挂载到Colab,如图this gist
更新(2020 年 4 月):现在您可以自动安装 Google 驱动器。从云端硬盘复制比上传更容易。
- 将
mylib.py
存储在您的云端硬盘中 - 打开一个新的 Colab
- 打开(左)侧窗格,select
Files
查看 - 点击
Mount Drive
然后Connect to Google Drive
- 复制
!cp drive/MyDrive/mylib.py .
import mylib
试试这个方法:
我有一个名为 plant_seedlings 的包。该包存储在 google 驱动器中。我应该做的是将这个包复制到 /usr/local/lib/python3.6/dist-packages/.
!cp /content/drive/ai/plant_seedlings.tar.gz /usr/local/lib/python3.6/dist-packages/
!cd /usr/local/lib/python3.6/dist-packages/ && tar -xzf plant_seedlings.tar.gz
!cd /content
!python -m plant_seedlings
- 您可以使用 google.colab.filesupload() 函数将本地文件上传到 google colab
- 如果您在 github 上有文件,请使用
!git 克隆 https://github.com/username/repo_name.git。然后就像在 jupyter notebook 中一样使用魔术函数加载它 %load
%load filename.py
.
根据 Korakot Chaovavanich 的回答,我创建了以下函数来下载 Colab 实例中所需的所有文件。
from google.colab import files
def getLocalFiles():
_files = files.upload()
if len(_files) >0:
for k,v in _files.items():
open(k,'wb').write(v)
getLocalFiles()
然后您可以使用通常的 'import' 语句将您的本地文件导入 Colab。希望对您有所帮助
您可以将这些 .py 文件上传到 Google 驱动器并允许 Colab 使用它们:
!mkdir -p drive
!google-drive-ocamlfuse drive
根文件夹中的所有文件和文件夹都将位于 drive
。
现在是 2019 年 6 月。
确保在 Python 包的 __init__.py
中所有相关文件都按顺序导入。将代码推送到 Git 或使用
例如,
from .Boxes import *
from .Circles import *
from .Rectangles import *
...
不要使用 __init__.py
文件中的包名称来导入文件。
在 Google colab 中,
! rm -rf SorghumHeadDetection
! git clone https://github.com/user/amazing-repo-name/
我遇到了同样的问题。在阅读了无数post之后,我想介绍以下我最终选择的解决方案(例如使用urllib
、httpimport
、从[=59=克隆]、打包安装模块等)。该解决方案利用 Google 驱动器 API (official doc) 进行适当的授权。
优点:
- 简单安全(无需代码处理文件操作异常and/or额外授权)
- 模块文件由 Google 帐户凭据保护(没有其他人可以 view/take/edit 它们)
- 您可以控制 upload/access 的内容(您可以 change/revoke 在逐个文件的基础上随时访问)
- 一切尽在一处(无需依赖或管理其他文件托管服务)
- 自由 rename/relocate 模块文件(不基于路径,不会破坏 your/other 的笔记本代码)
步骤:
- 将您的 .py 模块文件保存到 Google 驱动器 - 您应该拥有它,因为您已经在使用 Colab
- 右击"Get shareable link",复制“
id=
”后面的部分——GoogleDrive 分配的文件id
- 将以下代码片段添加并运行到您的 Colab 笔记本中:
!pip install pydrive # Package to use Google Drive API - not installed in Colab VM by default
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth # Other necessary packages
from oauth2client.client import GoogleCredentials
auth.authenticate_user() # Follow prompt in the authorization process
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
your_module = drive.CreateFile({"id": "your_module_file_id"}) # "your_module_file_id" is the part after "id=" in the shareable link
your_module.GetContentFile("your_module_file_name.py") # Save the .py module file to Colab VM
import your_module_file_name # Ready to import. Don't include".py" part, of course :)
旁注
最后但同样重要的是,我应该相信这种方法的 original contributor。 post 可能在代码中有一些拼写错误,因为它在我尝试时触发了错误。经过更多阅读和故障排除后,我上面的代码片段有效(截至今天 Colab VM OS:Linux 4.14.79)。
以防其他人有兴趣知道如何从 google colab 中的 gdrive 导入 files/packages。以下程序对我有用:
1) 在 google colab 中安装您的 google 驱动器:
from google.colab import drive
drive.mount('/content/gdrive/')
2) 使用 sys:
将目录附加到您的 python 路径import sys
sys.path.append('/content/gdrive/mypythondirectory')
现在您应该可以从该目录导入内容了!
一个简单的方法是
- 输入 从 google.colab 导入文件 上传 = files.upload()
- 复制代码
- 粘贴到 colab 单元格中
我们可以做到。
import sys
import os
py_file_location = "/content/drive/My Drive"
sys.path.append(os.path.abspath(py_file_location))
现在您可以将其作为模块导入到该位置的笔记本中。
import whatever
以下是对我有用的步骤
在 google colab
中安装您的 google 驱动器从 google.colab 导入驱动器 drive.mount('/content/drive')
插入目录
导入系统 sys.path.insert(0,'/content/drive/我的Drive/ColabNotebooks')
检查当前目录路径
%光驱/我的Drive/ColabNotebooks %密码
导入您的模块或文件
进口my_module
如果出现以下错误'Name Null is not defined',请执行以下操作
5.1 从 colab 下载 my_module.ipynb 作为 my_module.py 文件(文件->下载 .py)
5.2 上传*.py文件到drive/MyDrive/ColabNotebooks in Google drive
5.3 导入 my_module 现在可以使用了
这是我的过程:
import sys
sys.path.insert(0, '/content/drive/MyDrive/my_folder')
%cd /content/drive/MyDrive/my_folder
%pwd
现在,您可以使用 import my_module
轻松地从该路径导入模块
您可以通过将驱动器挂载到 colab 并编写一些代码来放置 python 文件的 ID 来完成此操作 你可以在这里找到代码 importing python file from drive to colab
# Code to read file into colaboratory:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
#Autheticate E-Mail ID
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
#2.1 Get the file
your_module = drive.CreateFile({"id": 'write your file id here'}) # "your_module_file_id" is the part after "id=" in the shareable link
your_module.GetContentFile("write the file name here") # Save the .py module file to Colab VM
import file_name
from file_name import anything #as classes or functions from your file
os.listdir
可以用来查看目录下的所有文件
from google.colab import drive
drive.mount("/content/drive")
import os
path="/content/drive/My Drive/Colab Notebooks"
os.chdir(path)
os.listdir(path)`
就我而言,我尝试使用的文件名为 client.py
。这引起了冲突,因为 /usr/local/lib/python3.7/dist-packages/
.
client
的库
我通过将 client.py
文件上传到保存 Colab Notebook 的同一 Google Drive 文件夹并将其名称更改为未出现在 dist-packages
文件夹。
在我的例子中,我将文件名更改为 dfsclient.py
然后用
import dfsclient
然后我实现了 Kamal 的答案:
import sys
sys.path.insert(0, '/content/drive/MyDrive/my_folder')
我经常这样做:
将我的模块保存在目录中。在 MyModules
中说 MyFile.py定义我的模块的位置:
path_m = '/content/drive/MyDrive/Colab Notebooks/MyModules/'
然后我把路径加到sys.path:
import sys sys.path.insert(0,path_m)
import the module into my Jupyter/Google Colab notebook.
import MyFile