有没有办法上传我的.py文件并在colab代码单元格中导入它们?
我找到的另一种方法是创建本地Jupyter笔记本,然后将其上传到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月):现在您可以通过以下方式轻松上传:
更新(2019年10月):如果您不想每次都上传,您可以将其存储在 S3 中并将其挂载到 Colab,如 此处的 gist 所示。
更新(2020年4月):现在您可以自动挂载 Google Drive。从驱动器复制它比上传更容易。
mylib.py
存储在您的云端硬盘中!cp drive/MyDrive/mylib.py .
命令将其复制到Colab中import mylib
如果有其他人想知道如何在Google Colab中从Google Drive导入文件/程序包,请参照以下步骤:
1)在Google Colab中挂载您的Google Drive:
from google.colab import drive
drive.mount('/content/gdrive/')
2) 使用sys将目录添加到你的Python路径中:
import sys
sys.path.append('/content/gdrive/mypythondirectory')
现在你应该能够从那个目录导入内容了!
%load filename.py
加载它。%load repo_name/filename.py
- xyzjayne根据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中导入您的本地文件。希望这可以帮助您。
您可以使用常规的“import”语句在Colab中导入您的本地文件。希望这可以帮助您。
import sys
import os
py_file_location = "/content/drive/My Drive"
sys.path.append(os.path.abspath(py_file_location))
import whatever
我也遇到了同样的问题。在阅读了大量的帖子后,我想介绍一下我最终选择的以下解决方案(例如使用urllib
、httpimport
、从GitHub克隆、打包模块进行安装等等方法)。该解决方案利用了Google Drive API (官方文档)进行适当授权。
id =
”后面的部分-由Google Drive分配的文件ID!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 :)
最后,我应该要感谢这个原始贡献者提供的方法。那篇文章可能存在代码错误,当我尝试时它会触发一个错误。通过更多阅读和排除故障,我的代码段上面的方法可以正常工作(在Colab VM操作系统:Linux 4.14.79上测试成功,截至今天)。
可以尝试这种方法:
我有一个名为plant_seedlings的包,它存储在Google Drive中。我需要做的是将此包复制到/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
import sys
sys.path.insert(0, '/content/drive/MyDrive/my_folder')
%cd /content/drive/MyDrive/my_folder
%pwd
import my_module
从该路径导入模块。这是我经常的做法:
将我的模块保存在目录中。例如:MyModules文件夹下的MyFile.py
定义我的模块位置:
path_m = '/content/drive/MyDrive/Colab Notebooks/MyModules/'
然后将路径添加到sys.path中:
import sys
sys.path.insert(0,path_m)
在我的Jupyter/Google Colab笔记本中导入模块。
import MyFile
!cp drive/MyDrive/mylib.py .
的末尾添加.
。不管怎样,这是一个干净而美好的解决方案。解决了我的大问题。非常感谢。此外,我真的很感激您随时更新答案中的colab。真的非常感谢。 - Mainland