Fastai import best practice

I’d like to ask the community on what the best practice is for importing various fastai libraries? In most of the courses, I see that we import everything with a wildcard without aliases (ie from import *).

I know this is personal preference, but this makes it less obvious where certain methods/classes come from while learning. Is there a specific reason why courses choose this kind of imports instead of doing import as fv? Happy to read any further material on this.

1 Like

In the first chapter of the fastai book, Jeremy states,

A lot of Python coders recommend avoiding importing a whole library like this (using the import * syntax), because in large software projects it can cause problems. However, for interactive work such as in a Jupyter notebook, it works great. The fastai library is specially designed to support this kind of interactive use, and it will only import the necessary pieces into your environment.

Hope that helps.

1 Like

I prefer doing imports such as the ones shown in my course here: Lesson 2 - Image Classification Models from Scratch | walkwithfastai

Such as:

from import *

Is replaced with:

from torch import nn

from fastai.callback.hook import summary
from fastai.callback.schedule import fit_one_cycle, lr_find 
from fastai.callback.progress import ProgressCallback

from import Datasets, DataLoaders, show_at
from import untar_data, URLs
from import Categorize, GrandparentSplitter, parent_label, ToTensor, IntToFloatTensor, Normalize

from fastai.layers import Flatten
from fastai.learner import Learner

from fastai.metrics import accuracy, CrossEntropyLossFlat

from import CropPad, RandomCrop, PadMode
from import PILImageBW
from import get_image_files

Though I usually do this only after I’m all done hacking away and I push my open source code, for readability

1 Like