How to import functions?

If I have to use a layer like simple_cnn I import from fastai.layers import *
If I have to use ItemList I import from fastai.data_block import *

So my question is why from fastai import * does not work (or what does it do, it does not import anything)? Why I have to import all files independently?

How I installed fastai?

  1. Created a new pytorch env conda create -n PyTorch anaconda
  2. Installed pytorch conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
  3. Installed fastai conda install -c pytorch -c fastai fastai pytorch

I have tested this on both terminal(in ipython) and jupyter lab.

I’m not entirely sure on this. I think the relevant sections are and

What I think is going on is:

  1. from fastai import * : binds all submodules to the fastai namespace so that they can be referenced as fastai.XXXXX (where XXXX is submodule name like vision or tabular). But I don’t think it makes all functions available inside each submodule. Any functions that don’t reside in a submodule, i.e. inside the higher level fastai parent module are available though.
  2. from fastai.layers import * based on the submodules link seems to implicitly load the fastai parent module. To me this means that writing from fastai.layers import * is the same as writing: from fastai import * and from fastai.layers import *. In that situation it seems from fastai import * is superfluous.

I used this link to help me get acquainted with modules:

1 Like

I am kind of getting your point.

The behavior of from x import * is defined by whether or not you are importing from a package, a module or a subpackage of the parent package. Modules are *.py files (e.g., whereas subpackages are child directories of the parent package (e.g.

If you’re importing a package or a subpackage, the default behavior is defined by the file in that directory. In addition to specified imports in this file, the __all__ variable can specify additional packages for import. See the example below from the subpackage.

In the case of from fastai import *, the fastai/ file does not explicitly specify any imports or an __all__ variable.



In the case of from fastai import * , the fastai/ file does not explicitly specify any imports or an __all__ variable.

That’s helpful. I just screenshot-ed my fastai directory. I think based on that the from fastai import * buys you all of the *.py files and respective functions you can see in the screenshot (i.e. basic_data, basic_train, basics, callback, etc…) that aren’t in the subdirectories/subpackage/submodule. So is minimally functional with fastai import * but you can’t use the tabular, text, vision subpackages unless you specifically import them.


On thanks I got it.