Search code examples
pythonpython-importpython-module

Hierarchy in Packages


I'm doing simulations of optical properties and started to create a package for my own use that includes all functions and object I have written so far. For obvious reasons I use numpy and I used to import numpy in my modules.

Now, with the package, I wish to make numpy globally available for every submodule. From what I've gathered so far, this might be bad practice. However, if I import numpy in each module I get numpy as a key/function for that module, which seems to be even worse practice.

In other words: When I create a minimal package with only package/__init __.py and package/module.py and import numpy in module.py, import it and print all available functions

import package
print package.module.__dict__.keys()
['__builtins__', '__file__', '__package__', 'numpy', '__name__', '__doc__']

numpy shows up. So in principle I could access numpy via

package.module.numpy

I am not sure if this is really a problem and how this is done correctly.


Solution

  • I agree with @Sneftel's comment -- this isn't a problem.

    Since this is not part of the documented interface of your module, although the user can see there is something named numpy in the module (which is not necessarily the numpy module, it could be anything), as a grownup, the user should avoid using it.