Ann
Ann

Reputation: 75

Google Colab does not apply the changes I make to the code in my local directory

I am working with a cloned repo from git for a project, and I am making changes to the cloned files constantly. I ran into a problem recently: no matter what I change in the local files from the cloned github project, those changes wouldn't apply to the colab notebook (for example if I add a print here and there, nothing would print, but the functions are called).

I began facing this problem after trying to push all my files from my colab to git, because I know they are not being saved if I only save the notebook, and I followed the commands from here:

https://navan0.medium.com/how-to-push-files-into-github-from-google-colab-379fd0077aa8

I am not sure, but I might have started some multithreading, and I am not good at dealing with that. No matter what I change, the changes won't appear to have taken place, and I must push the changes to git and then clone it again and then they would appear. Also, I must terminate the runtime, because I cannot get rid of the directory with !rm -rf directory_name, it will still appear in files. If I re-run same command, it says directory does not exist.

I am pretty sure it's multithreading or some forked processes. I would like to terminate that.

Link to my notebook: https://colab.research.google.com/drive/1WMaDcEwOdPEsZL65nU_T4Ps9tIkyPY5v?usp=sharing

Upvotes: 5

Views: 8139

Answers (1)

Craig Citro
Craig Citro

Reputation: 6625

There's nothing colab-specific going on here -- any long-lived Python session would behave the same way.

The issue you're hitting is that python imports are idempotent: once you import foo, any further attempts to import foo are a no-op. (Python caches the results of imports in sys.modules.) So if you import a module, edit it, and import again, you won't see your edits.

You'd see the same if you were in the python REPL, and attempted to edit a file and re-import it.

You have two easy ways out:

  • restart the python process; in Colab, this is ctrl-m . or Runtime -> Restart runtime.
  • you can use importlib.reload to re-import your updated code.

reload is great, but I'll warn you that it has many sharp edges, and it's quite easy to trick yourself. If your setup is reasonably quick (eg you don't have to recreate a bunch of large objects in memory), restarting is a great way to maintain your sanity.

Upvotes: 9

Related Questions