Propagate import for all modules in a package.

Discussion in 'Python' started by Phil, Jul 17, 2009.

  1. Phil

    Phil Guest

    I'm really new to Python and I am absolutely stumped trying to figure
    this out. I have searched plenty, but I am either searching for the
    wrong keywords or this isn't possible.

    What I want to do is have one import be global for the entire package.
    Here is an example...

    <package>
    __init__.py
    module1.py
    module2.py
    ...
    moduleN.py

    I was thinking that I could just, for example, 'from datetime import
    datetime' in __init__.py and have the ability to use 'datetime'
    anywhere in any of the modules in 'package'.

    This didn't work for me. Did I just do something wrong? Is what I am
    trying to do possible?

    Thanks in advance.
     
    Phil, Jul 17, 2009
    #1
    1. Advertising

  2. Phil schrieb:
    > I'm really new to Python and I am absolutely stumped trying to figure
    > this out. I have searched plenty, but I am either searching for the
    > wrong keywords or this isn't possible.
    >
    > What I want to do is have one import be global for the entire package.
    > Here is an example...
    >
    > <package>
    > __init__.py
    > module1.py
    > module2.py
    > ...
    > moduleN.py
    >
    > I was thinking that I could just, for example, 'from datetime import
    > datetime' in __init__.py and have the ability to use 'datetime'
    > anywhere in any of the modules in 'package'.
    >
    > This didn't work for me. Did I just do something wrong? Is what I am
    > trying to do possible?


    Each module has it's own namespace, there is no real global one. So
    there is no obvious and recommended way to do what you want.

    You can do it through, by stuffing names into the __builtins__-module,
    which acts as "namespace of last resort"


    from datetime import datetime
    __builtins__['datetime'] = datetime

    However, this is strongly discouraged - you can easily get
    name-conflicts, and undefined behavior in your modules if you rely on this.

    Diez
     
    Diez B. Roggisch, Jul 17, 2009
    #2
    1. Advertising

  3. On Fri, 2009-07-17 at 10:28 -0700, Phil wrote:
    > I'm really new to Python and I am absolutely stumped trying to figure
    > this out. I have searched plenty, but I am either searching for the
    > wrong keywords or this isn't possible.
    >
    > What I want to do is have one import be global for the entire package.
    > Here is an example...
    >
    > <package>
    > __init__.py
    > module1.py
    > module2.py
    > ...
    > moduleN.py
    >
    > I was thinking that I could just, for example, 'from datetime import
    > datetime' in __init__.py and have the ability to use 'datetime'
    > anywhere in any of the modules in 'package'.
    >
    > This didn't work for me. Did I just do something wrong? Is what I am
    > trying to do possible?
    >


    That's not how packages (were designed to) work. A package is basically
    a namespace. It doesn't do anything special outside of providing a
    namespace for common modules. There are some special features (e.g.
    __init__.py which is basically the "body" of the package and '__all__'
    which handles wildcard imports of a package) but other than that it's
    just a namespace.
     
    Albert Hopkins, Jul 17, 2009
    #3
  4. Phil

    Phil Guest

    Thanks to both of you for the fast and detailed responses. I will just
    treat the package for what it is, a namespace.
     
    Phil, Jul 17, 2009
    #4
  5. Phil

    Phil Guest

    Thanks to both of you for the fast and detailed responses. I will
    just
    use the package for what it is, a namespace.
     
    Phil, Jul 17, 2009
    #5
  6. Phil

    Phil Guest

    Thanks to both of you for the fast and detailed responses. I will just
    use the package for what it is, a namespace.
     
    Phil, Jul 17, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. greg
    Replies:
    1
    Views:
    318
    Morgan
    Dec 24, 2003
  2. Dave
    Replies:
    2
    Views:
    471
  3. Disc Magnet
    Replies:
    0
    Views:
    165
    Disc Magnet
    Apr 18, 2011
  4. Propagate Identity to WebService

    , May 30, 2008, in forum: ASP .Net Security
    Replies:
    1
    Views:
    119
    Joe Kaplan
    May 30, 2008
  5. Replies:
    6
    Views:
    265
    Steve Callaway
    Jul 19, 2005
Loading...

Share This Page