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 https://docs.python.org/3/reference/import.html#submodules and https://docs.python.org/3/reference/import.html#regular-packages

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: https://www.digitalocean.com/community/tutorials/how-to-import-modules-in-python-3

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. data_block.py, whereas subpackages are child directories of the parent package (e.g. fastai.vision).

If you’re importing a package or a subpackage, the default behavior is defined by the __init__.py 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 fastai.vision subpackage.

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

References:
https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html
https://docs.python.org/3/tutorial/modules.html#importing-from-a-package

4 Likes

In the case of from fastai import * , the fastai/__init__.py 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 fast.ai is minimally functional with fastai import * but you can’t use the tabular, text, vision subpackages unless you specifically import them.

2 Likes

On thanks I got it.