Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "header.py" module

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

I've tried to create a header.py file where I just pasted the common header. I've then added from header import * on the other ones, but it seems to me that this would only import the methods listed on header.py, rather than the actual modules.

I know one solution is to figure out which libraries each small file depends on, but isn't there a faster way to do that?

like image 758
Alexandre Cordeiro Avatar asked Nov 28 '13 12:11

Alexandre Cordeiro


1 Answers

It is very important and useful to have all dependencies in a .py file to be listed in the import statements. That way we can easily trace back the source of all modules used.

Say if you are using module1.method somewhere and want to check where this method came from, you'll always find in the import statements at the top. This is the reason why from module1 import * is highly discouraged.

We can do what you need in neat way.

In your header.py

import module1
import module2

In your other files,

import header

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

If you think its making your code writing tedious because of longer function names, then may be try this.

import header as h
h.module1.method()

But please make sure you don't have untraceable modules in your python files.

like image 137
Sudipta Avatar answered Oct 05 '22 23:10

Sudipta