Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange import issue --> AttributeError: module 'captum' has no attribute 'attr' #680

Closed
adaruna3 opened this issue Jun 11, 2021 · 3 comments

Comments

@adaruna3
Copy link

🐛 AttributeError: module 'captum' has no attribute 'attr'

When importing captum with just import captum, the package is missing modules like attr. Running dir(captum) shows that the other modules, like attr are missing. However, if a statement like from captum.attr._utils.input_layer_wrapper import ModelInputWrapper is included after import captum, the captum package then has the missing modules. Again, verified by running a dir(captum). Is this expected behavior?

To Reproduce

Steps to reproduce the behavior:

  1. Run the code below:
import captum
print(dir(captum))
print("---------------")
from captum.attr._utils.input_layer_wrapper import ModelInputWrapper
print(dir(captum))
exit()
  1. The above should get an output of the following:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__']
---------------
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_utils', 'attr', 'log']

Expected behavior

I assume all modules should be available after import captum. If this behavior is not expected, please close and disregard.

Environment

I installed captum using the suggested conda method. See details below. After noticing this issue in an environment I was developing in with many other packages, I made a new, clean environment that only contained captum and any dependencies conda installed. Same issue.

  • Captum / PyTorch Version (e.g., 1.0 / 0.4.0): 0.3.1
  • OS (e.g., Linux): Ubuntu 18.04.4 LTS
  • How you installed Captum / PyTorch (conda, pip, source): conda
  • command: conda install captum -c pytorch
  • Python version: 3.6.13
@NarineK
Copy link
Contributor

NarineK commented Jun 24, 2021

Thank you for raising the issue, @adaruna3 ! We will look into it.

@vivekmig
Copy link
Contributor

Hi @adaruna3 , I think this is likely expected behavior, since importing a module doesn't automatically import all submodules. This would only occur if the main captum init imported the specific submodules, which is not currently the case, so you would need to either do import captum.attr or from captum.attr import * to import the attr submodule. These links also discuss similar issues (1, 2)

@adaruna3
Copy link
Author

Hi Vivek, totally understand. Ultimately it depends on the design choices of the developers. I am used to importing the main package captum, torch, or numpy for example, and then expect to be able to access the highly utilized modules intended for public use within that package. Like with torch, I import torch but have access to nn, multiprocessing, optim, etc. and with numpy I have access to random, linalg, etc

I just mentioned it in case that behavior was not caught yet. But if having an import for each module in the package is intended design, that works too. Thanks!!

facebook-github-bot pushed a commit that referenced this issue Jun 10, 2022
Summary:
This fixes issue #680 Strange import issue --> AttributeError: module 'captum' has no attribute 'attr'

In most python packages, you can import the toplevel package, like numpy, scipy, torch, etc.. and then access the submodules simply by the dot-operator. Like you can use `import numpy` and after that you can use any submodules à la `numpy.random.uniform`.

With this PR, you can just `import captum` and then for example use `captum.attr.DeepLift` or `captum.robust.Perturbation` instead of having to import both. It's just a small convenience, and I think there are more people that expect this kind of import to work but don't bother to create an issue out of this.

I hope this PR is considered as helpful.

Pull Request resolved: #912

Reviewed By: NarineK

Differential Revision: D37053826

Pulled By: vivekmig

fbshipit-source-id: 64fa2be7651ca30571d1eb85b45dd11410676c4b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants