What happens when importing modules in python?

I want to know what happens when we import a module file in python. I mean, this is a process, in other words, what kind of python things will be running or tested ?! e.g. __init__.py or sys.modules etc. for example, I know that __init__.py is the necessary files in each package, I want to know python what to do with these files during import? please light it for me.

+4
source share
1 answer

Read the section on modules , the documentation of the import statement, imp module (in particular, examples) and, possibly, the documents for __import__ inline . It has a long way to go. If you still want to know more, I would suggest asking a specific question, this is a bit on the broad side.

Change After reading your question again, there is a certain part to your question about what __init__.py does in packages. It can be empty or contain the initialization code that will be executed when this package is imported. See the section on packages for more details.

In __init__.py you can also set __all__ , which determines which characters are imported when you do from yourpackage import * . This is explained in detail in importing * from a package .

+7
source

Source: https://habr.com/ru/post/1442836/


All Articles