Google Colab creative process.

How to solve ‘Mountpoint must either be a directory or not exist’ in Google Colab workbook?

4 min read.

This article is about how to fix a common error in Google Colab notebooks when trying to mount a Google Drive, and you just get an error along the lines of ‘Mountpoint must either be a directory or not exist’. I’ll explain the reason for the error, and provide several solutions ranging from stupidly simple to normally simple.

Yeah – no space surgery available in this article. Because sometimes simple solutions are the best 😉

Background

So I was executing the steps in a Google colab notebook following a (mildly outdated) tutorial, trying to fine-tune a Stable Diffusion model. But it wasn’t going that well.

See, I first ran the steps once, ran into issues in a later step, struggled a bit to fix them, as part of that process destroyed some uploaded files from my Google Drive and refreshed the page. At that point, I figured I’d just start again.

And then trouble found me. But before digging into the error, let’s take a step back and talk about the tech for a bit.

Basics

Google Colab is a free online service that allows you to write and execute Python code in your browser, with access to GPUs and other features. You can use Colab to create and share notebooks, perform data analysis, train machine learning models, and more. Colab is based on the Jupyter Notebook project, and supports many of its features and extensions

One of the many Google Colab use cases is to use it to fine-tune Stable Diffusion models for your own use. And that’s exactly what I was doing.

Problem

So when executing a step that does something like this:

from google.colab import drive
drive.mount('/content/gdrive/')

You get an error like this:

ValueError                               Traceback (most recent call last)
<ipython-input-45-9667a744255b> in <module>()
       1 from google.colab import drive
 ----> 2 drive.mount('content/gdrive/')

 /usr/local/lib/python3.6/dist-packages/google/colab/drive.py in
 mount(mountpoint, force_remount)
      99       raise ValueError('Mountpoint must either be a directory or not exist')
     100     if '/' in mountpoint and not _os.path.exists(_os.path.dirname(mountpoint)):
 --> 101       raise ValueError('Mountpoint must be in a directory that exists')
     102   except:
     103     d.terminate(force=True)

 ValueError: Mountpoint must be in a directory that exists

In the colab notebook the error looks somewhat like below:

But… Why? And how do I fix this?

Reason

Maybe I was just bad at googling or maybe this stuff is a tad confusing, but it wasn’t immediately obvious to me what was going wrong. But let me explain.

The error says “Mountpoint must either be a directory or not exist“, but what it means, is that the Virtual Machine you’re remotely running your Python code in, already has files in the path you’re trying to mount.

So that path you supply to drive.mount() is an absolute path in the machine’s file system, and you’re trying to mount a Google Drive to a directory which already has files.

Yes – the error comes from the VM. It’s not about the files in Google Drive (at least, not yet!)

Since we now know what’s up, we can proceed to fixing the issue!

Solution

A bunch of solutions, actually. And they’re roughly in the order of complexity, starting with the simplest one.

1. Skip the step altogether

This may or may not work, but in case you already executed this step successfully, and are now trying to re-run it – then the error is correct but you can just ignore it!

The mountpoint already exists, so you might want to try skipping the mounting step (or the part in your code that mounts the drive) altogether.

If that doesn’t work, jump to step 2.

2. Double-check your mounting path

Are you mounting to a path starting with “/”? Or are you trying to mount to some weird system paths? Or maybe you have some invalid characters in the path?

Either way, get rid of any oddness, and use some super basic path like “/content/gdrive” as your mountpoint. And then proceed to the next step (it’s also required).

3. Change the path(s) in your script

If step 2 helped you get past the part of your notebook where you’re mounting your Google Drive, you’ll now need to make sure you didn’t have paths referring to your old mountpoint in your script.

And if step 2 didn’t help you, you can change your path anyway. If you used “/content/gdrive” at first, change it to “/content/drive”. Or “/content/googledrive”. You probably get my point. And now you need to go through the rest of your script to make sure it’s reflected everywhere.

In case your script is long and complicated, or you just don’t like searching and replacing, there’s one more step you can try…

4. Have you tried turning it off and on again?

I’m not joking! Hear me out.

You’re executing code remotely in a virtual machine, and that virtual machine is not stateless. I mean, not when you’re still connected and the machine does not get destroyed and a new instance created.

A-HA! That’s it. Disconnect, destroy, and reconnect, and you’ll get a brand new machine without any leftover files – and your mountpoint should be available again.

You can do this by either clicking the little icon showing your virtual machine status and selecting “Disconnect and delete runtime”…

Or by clicking on “Runtime” and selecting, you guessed in, “Disconnect and delete runtime”:


And there you go! When you connect again, you should be able to run your notebook from the start again :)

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments