

この記事でわかること!
- Pythonの実行環境を最速で用意できるGoogle colaboratoryのメリットがわかる。
- Pythonの実行環境を最速で用意できるGoogle colaboratoryの初期設定方法がわかる。
超簡単。Python実行環境を『Google colaboratory』で爆速で準備しよう。
Google colaboratoryとは?
Google Driveの拡張機能であるGoogle colaboratory(以下:Google Colab(コラボ))について紹介します。Google Colabなら5分程度の初期設定をすれば、爆速で簡単にPythonスクリプトを実行することができます。
Python初学者こそ「Google colab」を使うべき!
プログラミングを学ぶことの一番のハードルとなるのがプログラミング環境の準備だと思っています。プログラミング実行環境のインストールと設定、エディタの設定、初学者にとってはとても時間がかかる作業です。

プログラミングを学ぼうと思った方の多くは、環境構築で挫折するそうです。

Google colabなら5分程度の簡単な初期設定を行えばその後はどのPCからでもすぐに、Python環境を呼び出すことができます。
Google colaboratoryのメリット!!
Google colaboratoryの良いところ♪
- 無料
- クラウド上のPython環境にブラウザから超絶簡単アクセスできる。
- デバイスによる環境の違いによるエラーに悩まされにくい
- ブラウザ上で動くため。
- WindowsでもMacでも、OS問わず同じ環境でPythonを実行できる。
- Google Driveと連携してGoogle Drive上のファイルを読み込んだり書き込んだりできる。
- 機械学習などの時間のかかる処理でも、Googleの高性能サーバ上のGPUで動作させることができる。
- Pythonスクリプトを簡単に共有できる。
- 基本的ならライブラリが最初からインストールされている。
Google colaboratoryの利用要件

Google colaboratoryを利用するには、Googleアカウントが必要です。個人アカウントでもGoogle Workspace(旧Google Suites)アカウントでもOKです。
Googleアカウントをお持ちでない方はこちらから新規作成しましょう。
(手順1) Google DriveにGoogle colaboratoryをインストールする。

▲「新規」をクリックします。

▲「その他」→「アプリを追加」をクリックします。

▲「Google Colab」で検索して表示される「Google colaboratory」アプリをクリックして、Googleドライブにインストールします。(画像はインストール済の状態の画像です。)

なんと、これだけで初期設定はおわりです。もうPython書けます。
(手順2)Google colaboratoryを使ってみる

さっそくPythonを書いてみましょう。

▲Googleドライブ画面の「新規作成」をクリックします。

▲その他→Google colaboratoryをクリックします。

▲「Untitled0.ipynb」というファイルが新規作成され開きます。
図の赤い枠がコードペインです。ここにPythonコードを書いていきます。
print("hello world.")
▲上記のコードを書いてみましょう。

▲実行のショートカットキーCtrl+Enter
で実行してみます。すると実行結果がコードブロックの下に表示されます。

スクリプトを実行することができました。ここからは好きなようにコードを書いて遊んでみてください。
Google colaboratoryファイルの保存場所

▲ファイルを保存してみましょう。保存のショートカットキーはCtrl+S
です。
保存に成功すると上図の赤枠のところに保存時間が表示されます。

▲Google colaboratoryのファイルは、「ipynb」という拡張子でGoogle Driveに保存されます。
次回このファイルを編集するときは、ダブルクリックして開きます。
Google colaboratoryの使い方について学ぶ
Google colaboratoryはJupyter Notebookというソフトウェアがベースになっています。基本的な使い方はJupyter Notebookの使い方について検索すると良いかと思います。(若干の違いはあります。)
関連リンク
- Jupyter Notebook公式ドキュメント
- Google Claboratoryの開始方法(Coding TensorFlow)(Google公式の紹介Youtube動画)
Jupyter Notebookの特徴
Jupyter Notebookの特徴
- セルというブロック単位でpythonコードが実行できる。
- Markdown記法が使える。
PythonでGoogle Driveを操作しよう!
まず最初にやるべきGoogle Driveとの接続方法

Google Colabの初期設定が終わったらGoogleドライブと接続しましょう!

5秒でおわるかんたんな設定だよ!
Google colabでPythonスクリプトを実行する場合、はじめにGoogle ColabとGoogle Driveを接続すると大変便利です。というのも、Google Colabではスクリプトの実行結果をファイルに出力する場合、デフォルトの出力先は仮想ドライブになっているのですぐに削除されてしまいます。Googleドライブに出力することでファイルを保存することができます。
Google ColabとGoogleドライブを接続するメリット!
- Google Driveに保存しているファイルをスクリプトで読み込むことができる。
- スクリプトからGoogle Driveにファイルを書き出すことができる。

Googleドライブと接続する方法は下記の記事をご覧ください!

Googleドライブ上のExcelファイルを操作する方法

Pythonを学ぶなら

▲Python学習として独学よりデイトラが良かったです。受講レビューをまとめてみましたのでこれからPythonを学ぼうと考えている場合はご覧ください。
先着1000名まで1万円引きキャンペーン実施中!(8月31日まで)
\買い切りだからコスパ最高・永久にユーザーコミュニティ参加可能/
関連記事
Google colabではgood



さいごに

以上、Google colaboratoryのはじめかたについて、ご紹介しました。

次回は、より便利に使うためにGoogle DriveをマウントしてGoogle Drive内のファイルをPythonで扱う方法をご紹介します。
▲Python入門者向けの書籍です。デスクワークの業務効率化方面を中心に自動化するスクリプトを作成することができます。Excel・Word・PDF・デスクトップアプリ化・メールなどなど。身近な作業を自動化しながら学べるので事務員の方やエンジニアの方幅広くお勧めできます。
▲Pythonでデータ分析するのに超絶おすすめです。データ分析でよく使うPandasモジュールを中心にデータ加工から分析までの基礎を理解できます。
▲Pythonのお作法なんかが書かれています。