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

Terry Reedy tjreedy at udel.edu
Tue May 11 15:25:43 EDT 2010


On 5/11/2010 8:04 AM, Auré Gourrier wrote:
> Dear all,
>
> I am building a library package of the form:
>
> rootlib
> ---__init__
> ---subpackage1
> ------__init__
> ------sub1module1
> ------sub1module2
> ------...
> ---subpackage2
> ------ __init__
> ------sub2module1
> ------sub2module2
> ------...
>
> My rootlib.__init__ file contains:
>
> __name__        = ...
> __version__     = ...
> __author__      = ...
> __author_email__= ...
> __url__         = ...
> __description__ = ...
>
> import subpackage1
> import subpackage2
>
> __all__ = []
> __all__.extend(['subpackage1','subpackage2'])
>
> 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 (?). In the end, I would like to make this
module a "global" module for my library.

I have a similar package structure and after writing a few submodules, I 
developed a template that includes common text, including a couple of 
imports that are usually needed. I also made a backup copy of the 
template in case I open the template and forget to 'save as 
<submodule-name>' before just 'save'ing ;-).

If I had several such imports, and thought the list might expand, I 
might make one submodule for imports and then do 'from rootlib.util 
import importmod as m' in the template. But I have no need now for such.

Terry Jan Reedy





More information about the Python-list mailing list