Python module "header.py"

I split one large python file containing a bunch of methods into several smaller ones. However, there is one problem: I would like all these small files to import almost the same modules.

I tried to create a header.py file where I just inserted a common header. Then I added from header import * to others, but it seems to me that this will only import the methods listed in header.py , not the actual modules.

I know that one solution is to find out which libraries each small file depends on, but is there a faster way to do this?

+6
source share
1 answer

It is very important and useful to have all the dependencies in the .py file that will be listed in the import statements. Thus, we can easily track the source of all used modules.

Tell me, if you use module1.method somewhere and want to check where this method came from, you will always find it in the import statements at the top. This is why from module1 import * very discouraged.

We can do what you need carefully.

In your header.py file

 import module1 import module2 

In other files

 import header header.module1.method() #make all the function calls via header module. 

If you think this makes your code more tedious due to longer function names, you can try this.

 import header as h h.module1.method() 

But please make sure your python files have no traceable modules.

+5
source

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


All Articles