LoginSignup
298
216

More than 3 years have passed since last update.

ColaboratoryでのGoogle Driveへのマウントが簡単になっていたお話

Last updated at Posted at 2018-09-18

結論

以下のプログラムを使うと、drive/MyDrive/のなかにgoogle driveのデータがはいる。
※以前まで、MyとDriveのあいだに半角スペースがありましたが、必要なくなったので注意です

マウント
from google.colab import drive
drive.mount('/content/drive')

修正(2020/9/8)

さらに簡単になり、左上のフォルダマークから「ドライブをマウント」をクリックするだけでマウント完了するようになりました。

image.png

ドライブのマウントボタンを押すと…
image.png

アクセスを許可するか聞かれるので、「Googleドライブに接続」をクリックして認証に進む
image.png

認証後、ドライブのマウントボタンに斜線が入り、driveフォルダ配下に「My Drive」がマウントされています
image.png

ここから、フォルダ/ファイルのパスもコピーできるので、便利です!
image.png

※場合によっては、クリックしてもマウントできず、マウントするためのセルの実行を求められることがあります。その場合は、言われた通り現れたセルを実行しましょう!
(参考)
image.png

前提

私は、研究でGoogleのcolaboratoryを使用しています。
(研究室で使えるGPUがないからね!)

なので、google driveに学習するデータをあげて、マウントして、colaboratoryで読み込みしてました。
以下がいままで行っていたマウントの方法のプログラムです。

マウント(修正前)
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

!mkdir -p drive
!google-drive-ocamlfuse drive

これで2段階認証して、無事できていました。

エラー発生

今日もいつもどおり、マウントしようとしていたところ、、、あれエラー??
何も変えてないのに、、、

Processing triggers for dbus (1.10.22-1ubuntu1) ...
gpg: keybox '/tmp/tmpiruc_elw/pubring.gpg' created
gpg: /tmp/tmpiruc_elw/trustdb.gpg: trustdb created
gpg: key AD5F235DF639B041: public key "Launchpad PPA for Alessandro Strada" imported
gpg: Total number processed: 1
gpg:               imported: 1
Warning: apt-key output should not be parsed (stdout is not a terminal)
E: Unable to locate package google-drive-ocamlfuse

Unable to locate package google-drive-ocamlfuseとでてきてうまくいきません。
以上のエラーをGoogle検索しても、解決方法が出てきません。

マウントの方法が変わったのかと思い、
colaboratory gooogle drive」で検索しても上記の方法しかでてきませんでした。
Google アカウントを新しく作り直してもまったく変わらず、、

解決方法

諦めていたのですが、ColaboratoryのURLを開くと左にSample programがあるじゃないですか!!
sample programのExternal data:Drive, Sheet, and Cloud Storageにやり方が書いてありました。

以下が方法です。

マウント(修正後)
from google.colab import drive
drive.mount('/content/drive')

これだけです。たった2行で完了です。今まであんだけ長かったのに、、
実行すると、認証のURLが表示されます。
認証すると、drive/MyDrive/の中に、認証したアカウントのgoogle driveのファイルがはいっています。
drive直下でないのと、MyとDriveの間に半角スペースが空いている点が注意です!
※MyとDriveの間の半角スペースがなくなりました

これで、またいつものように研究できます。
一次情報をみることの重要性を再認識しました。

298
216
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
298
216