Modifying/hacking the fastai library

Hello!

I’m very new to developing libraries of any sort, but I’ve noticed some improvements I’d like to implement as well as some minor hacks I’ll need to do in order to get my project to work (things which are too specific to get an answer else wise), but I can’t seem to get any of the changes I do to the library to actually work in jupyter notebook.

So, how can I modify the library after my needs? Do i modify the code within the conda packages? Do i simply overwrite the functions locally in jupyter notebook? What is the “standard practice”, or at least a practice which works, for python libraries?

Thanks,

Lorentz

1 Like

Generally how I will do it (as I use Google Colab) is:

  1. I will change whatever file I need locally
  2. Upload to the current Colab instance
  3. Run a mv command to move my file to the FastAI library saved on Colab
  4. Restart the kernal, and import.

This allows me to use my custom functions I implement. If you’re not in colab, the steps would be the same. Find out where specific x file is saved, and update it.

Specifically, I modify the Fast.AI library like the train.py file for a few custom functions I use.

2 Likes

@muellerzr I see, I’ll try something similar. Thank you!

Another method is an editable install. Check out https://docs.fast.ai/dev/develop.html#development-editable-install. This is more convenient if you are looking to do a bit of work but is a bit more effort to get working. Depending on your setup you may need to do this into the particular virtual environment jupyter runs from. If you run import sys; sys.path in your notebook you can see which environment is used. Running import fastai; fastai.__path__ will show where fastai is located.

If you just want some changes for a specific notebook (or a couple of variants), rather than modifying the installed library you could just monkeypatch fastai. Though you may run into some caching issues with this method (looking around you’ll find some information on avoiding this, from a quick search the best I found was this which doesn’t look like a great explanation but gives some details).

2 Likes