Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading