Written by
Matt Bilyeu
on
on
Running an existing project on Google Colab
In the Deep Learning course I’m taking, I was describing how to run one of our projects on Google Colaboratory, when another student mentioned they’d been using Colab for over a year and didn’t know some of this information. I’m sharing it here in a short post to help others get up and running quickly and effectively with Colab.
Scenario: You have an existing IPython / Jupyter notebook with a directory of additional modules, and you’d like to run the project on Google Colaboratory (Colab).
Here’s how to do it.
- Upload project directory to Google Drive
- Navigate to notebook file, right-click and select “Open with” > “Google Colaboratory” to open the notebook
- If it’s your first time using Colab, you may need to search for it in the Google Workspace Marketplace
- If you want to use a hardware accelerator like GPU or TPU, go to “Edit” > “Notebook settings” and change the device
- Open the “Files” pane via the left-hand nav bar
- From there, select “Mount Drive” to connect to Google Drive. Follow the instructions and go through the auth flow.
- Install any additional packages you need by executing
!pip install <the requirements>
- Code cells beginning with an exclamation point will be executed as shell commands, e.g.
!ls
- Code cells beginning with an exclamation point will be executed as shell commands, e.g.
- Use the IPython magic command
%cd
to change the working directory to your project’s directory, e.g.%cd drive/MyDrive/path/to/project
- This will make it so the notebook uses the correct working direcotry when loading imports from other files and directories in your project
- You should now be able to load modules and run the rest of your notebook cells
- If you need to make changes to source files outside the notebook, you can (a) change them locally and re-upload them to drive or (b) open the files via the Colab Files section, and edit/save them directly. When edited outside of Colab, as in (a), you will need to restart the runtime or use
importlib.reload
. I’ve read that the%autoreload
magic can work here, too, but I haven’t tried it.
- If you need to make changes to source files outside the notebook, you can (a) change them locally and re-upload them to drive or (b) open the files via the Colab Files section, and edit/save them directly. When edited outside of Colab, as in (a), you will need to restart the runtime or use