I'm trying to write my first Python package, and almost all my modules will need to use NumPy. Should I write import numpy
in every single module or is there some place in the package I can just import it once so every module can use it? What's the best way to do this?
Asked
Active
Viewed 260 times
2

LWZ
- 11,670
- 22
- 61
- 79
-
I don't think there's much cost to writing `import numpy` at the top of each file. – askewchan Mar 06 '13 at 22:57
-
@askewchan, Thanks, I just want to make sure there's no redundancy. I don't want to import the same thing over and over again. – LWZ Mar 07 '13 at 08:37
-
1I still believe that the cost, including redundancy of reimporting, is lower than the lack of readability you might introduce with something more complicated. – askewchan Mar 07 '13 at 15:13
1 Answers
4
Yes, just import it everywhere it's needed.
Don't get too clever with writing functions that import everything for you or metamodules that import things and from which you import *
; all of that only serves to make your code hard to read and is just another place for bugs to happen.

Cairnarvon
- 25,981
- 9
- 51
- 65