pythonimportlibrariesnormalization

Importing a library in one file and using it in another file, without importing


There is a file named transforms.py, in it torchvision.transforms is imported and some custom transformations are defined. In another file named main.py, transforms.py is imported.

Now, in order to use torchvision.transforms.Normalize in main.py without importing it, Will it work (Normalize is not used in transforms.py, only imported)? And if it works, what's the reason behind it?

transforms.py:

from torchvision.transforms import *
...
Custom transformations defined
...

main.py

from data import transforms 
...
normalize = transforms.Normalize(mean=[0.5,0.5,0.5],std=[0.1,0.1,0.1])
...

Solution

  • Yeah, that should work. Reason being that import adds whatever you're importing to the namespace of the current file, which is exactly the same as what happens when you define a function, in that

    from module import a_function
    

    and

    def a_function:
    

    both end up with a_function defined. Either way, you can then import that file and access a_function with that_file_name.a_function()