handling modules in packages

Discussion in 'Python' started by Tommy Grav, Sep 5, 2007.

  1. Tommy Grav

    Tommy Grav Guest


    I am working on a package that contains a number of
    different modules:

    however, I find that several of the modules have the
    same import statements:


    import numpy
    import constants
    import conversion
    import observations


    import numpy
    import constants
    import conversions
    import transformations

    The modules themselves are not overly large, but it bugs
    me to have to import numpy twice (or even more as the
    number of modules grow). Is there a way to import numpy
    once in the package (like in the __init__.py file) such that
    it is accessible to all the modules? Or is the multiple imports
    just something one has to live with?

    Thanks for any help or direction to webpages discussing this

    Tommy Grav, Sep 5, 2007
    1. Advertisements

  2. Essentially, yes. That's the way it is, and it's better for understanding
    how things work in the respective submodules.

    However, you can stuff things into the __builtins__-module using setattr,
    and thus make names known globally.

    But it's a hack, and it means that you possibly create conflicts if
    different modules have different ideas on what is supposed to live under
    one key.

    so - don't do it. And live with the imports. After all, that's only c'n'p,
    and not of the bad kind.

    Diez B. Roggisch, Sep 5, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.