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

Fix subpackage imports and handle duplicate imports #17

Merged
merged 17 commits into from
Jan 17, 2025

Conversation

Y-T-G
Copy link
Contributor

@Y-T-G Y-T-G commented Jan 16, 2025

Subpackage imports should work now. Also added handling if packages are already imported.

from autoimport import lazy

with lazy():
    import torch.nn

torch.nn.Identity()
from autoimport import lazy
import torch.nn  # already imported 

with lazy():
    import torch.nn  # will return imported module instead of LazyLoader
from autoimport import lazy

with lazy():
    import torch.nn

torch.nn.Identity()

with lazy():
    import torch.nn  # no errors with duplicates 

torch.nn.Identity()

Y-T-G and others added 9 commits January 17, 2025 04:00
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
@glenn-jocher
Copy link
Member

glenn-jocher commented Jan 16, 2025

@Y-T-G ok I added a submodule import test but it's failing here. Normally this works:

import numpy.random
random_number = numpy.random.rand()

Test is showing:

with lazy():
    import numpy.random
random_number = numpy.random.rand()
    
Traceback (most recent call last):
  File "<input>", line 3, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'rand'

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
@glenn-jocher
Copy link
Member

@Y-T-G I added another submodule test that should be the same as your torch example but it's failing:

ERROR: test_submodule_imports_torch (test_autoimport.TestLazyImports.test_submodule_imports_torch)
Test torch module imports and functionality.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/autoimport/autoimport/tests/test_autoimport.py", line [36](https://github.com/ultralytics/autoimport/actions/runs/12817893794/job/35742124949#step:6:37), in test_submodule_imports_torch
    module = torch.nn.Identity()
             ^^^^^^^^
  File "/home/runner/work/autoimport/autoimport/autoimport/main.py", line 31, in __getattr__
    return getattr(self._module, attr)
AttributeError: module 'torch.nn' has no attribute 'nn'

@Y-T-G
Copy link
Contributor Author

Y-T-G commented Jan 16, 2025

@glenn-jocher Seems like the solution was much simpler. We just needed to return the loader of the parent package before the subpackage.

@glenn-jocher
Copy link
Member

Oh this looks great!

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
@glenn-jocher glenn-jocher merged commit e8210b8 into ultralytics:main Jan 17, 2025
7 checks passed
@Y-T-G Y-T-G deleted the subpackage-fix branch January 17, 2025 04:37
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

Successfully merging this pull request may close these issues.

2 participants