简体   繁体   English

从谷歌驱动器导入谷歌 Colab 中的模块 - python

[英]import module in Google Colab from google drive - python

I have a problem with import ipynb file from my google drive.我从我的谷歌驱动器导入 ipynb 文件时遇到问题。 I've tried solutions from: Importing.py files in Google Colab How to import custom modules in google colab?我尝试过以下解决方案: Importing.py files in Google Colab How to import custom modules in google colab?

but does not work for me.但对我不起作用。

# COLAB
from google.colab import files
from google.colab import drive
# SYS
import sys
# IPYNB
!pip install import-ipynb
import import_ipynb
# UTIL
import importlib.util

I've tried something like this:我试过这样的事情:

drive.mount('/content/drive')
sys.path.append('/content/drive/My Drive/Colab Notebooks/')
import Data_Preparation_Library

Or this:或这个:

!cp "/content/drive/My Drive/Colab Notebooks/Data_Preparation_Library.ipynb"
import Data_Preparation_Library

Here is how my gdrive structure looks like:这是我的 gdrive 结构的样子:

在此处输入图像描述

Thank You for answers in advance感谢您提前回答

SOURCE资源

Step 1 Primarily, you must Mount your google drive in google colab: Code to below, your files on your google drive is import files/packages inside a google colab.第 1 步首先,您必须在 google colab 中安装您的 google 驱动器:代码如下,您的 google 驱动器上的文件是在 google colab 中导入文件/包。

# Mount your google drive in google colab
from google.colab import drive
drive.mount('/content/drive')

Step 2 Secondly, insert the directory to your python path using sys:第 2 步其次,使用 sys 将目录插入 python 路径:

# Insert the directory
import sys
sys.path.insert(0,’/content/drive/My Drive/ColabNotebooks’)

Step 3 Now, you can be able to import your module or file stuff from that directory.第 3 步现在,您可以从该目录导入模块或文件。

# Import your module or file
import my_module

Worked version of Elci's solution for me: Elci 为我提供的解决方案的工作版本:

# Mount your google drive in google colab
from google.colab import drive
drive.mount('/content/gdrive')

# Insert the directory
import sys
sys.path.insert(0,'/content/gdrive/My Drive/Colab Notebooks')

# Import your module or file
import my_module

Huseyin Elci, I found a solution which worked for me: Huseyin Elci,我找到了一个对我有用的解决方案:

drive.mount('/content/drive')
sys.path.append('/content/drive/My Drive/Colab Notebooks/')
!cp -r "/content/drive/My Drive/Colab Notebooks/Data_Preparation_Library.ipynb" '/content/'

But I will try your approach Thank You for answer但我会尝试你的方法谢谢你的回答

import sys    

path_to_module = '/content/gdrive/My Drive/tmp'
sys.path.append(path_to_module)
print(sys.path)
import tmp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM