tags:

views:

100

answers:

2

Is there a way to share headers in python? i import the same things in different controllers in pylons.

+1  A: 

You could put the "header" things into a module of their own and then, wherever you need it, do

from myheadermodule import * 
sth
Except that that form of import is generally considered bad practice, for good reasons. If you are new to Python, you are likely to get yourself into trouble using it. If your program structure is such that you need it, you can probably do better by revisiting that structure and improving it.
Peter Hansen
@Peter: I'm not saying normal modules should generally be imported like that. I'm saying it would solve the specific described problem, which I understood as "how can I avoid to copy-paste the same `import` statements into lots of my modules". I don't think the normal reasons against `import *` would apply in this case. This interpretation of the question might be wrong though, since I don't really see how the accepted answer would solve that problem.
sth
@sth, you're quite right that your answer directly addresses the question. I'm just warning the OP that he should seek an alternative to using this approach. It's not necessary in any well-designed system. A simple variation that is much better would be using `import myheadermodule as mh` and then accessing everything inside as `mh.foo` and `mh.bar`. Feel free to incorporate the warning and advice into your answer, if you agree.
Peter Hansen
+3  A: 

Try putting the common code in the __init__.py file. As in here: "The __init__.py file is usually empty, but can be used to export selected portions of the package under more convenient names, hold convenience functions, etc."

kaloyan