how to import a module for global use in a library package ?

Jean-Michel Pichavant jeanmichel at sequans.com
Tue May 11 08:30:32 EDT 2010


Auré Gourrier wrote:
> [snip]
> My question is the following: I need to import an external package, 
> say numpy, for use in various submodules. So far, I simply do an 
> import numpy as _numpy where needed, say sub1module1 and sub2module2. 
> This means that I import this package a number of times which doesn't 
> seem to be a logical thing to do (?).
No problem at all.
You need numpy in a file, then import numpy at the very beginning of the 
file. You have 100 files requiring numpy? you'll end up with 100 'import 
numpy' statements, but who cares ? This is perfectly logic and pythonic 
in the way it is explicit.

You could reference numpy as member of you rootlib package, but any call 
of numpy methods will be prefixed by rootlib.numpy, and you still need a 
import rootlib (circular imports since rootlib is importing your 
submodules !)


JM

> Auré
>
>




More information about the Python-list mailing list