Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > question about importing a package

Reply
Thread Tools

question about importing a package

 
 
Matt
Guest
Posts: n/a
 
      12-06-2012
I have a directory structure that looks like this:

sample.py
sub_one/
__init__.py # defines only the list __all__ = ['foo', 'bar']
foo.py # defines the function in_foo()
bar.py # defines the function in_bar()

In sample.py, I have this command at the top:

from sub_one import *

I can't refer to in_foo() and in_bar() without prefacing them with the module names. I.e. foo.in_foo() and bar.in_bar() work, but I want to import them in the __main__ namespace of sample.py and refer to them as just in_foo()and in_bar(). I know this is frowned upon, but for my purposes it is the best choice. I have about 30 modules in my package (foos and bars) and I don't want 30 lines at the top of each file that uses this package. What am I doing wrong?

Thanks,

Matt
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      12-06-2012
On Thu, Dec 6, 2012 at 3:58 PM, Matt <> wrote:
> I have about 30 modules in my package (foos and bars) and I don't want 30 lines at the top of each file that uses this package. What am I doing wrong?


Not necessarily wrong, but definitely something to query: WHY do you
have thirty modules in your package? How big are your source files -
could you simply merge them into a single module?

ChrisA
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      12-06-2012
On 6 Dec, 14:58, Matt <m...@woodridgeadvisors.com> wrote:
> I have a directory structure that looks like this:
>
> sample.py
> sub_one/
> * * * __init__.py * * # defines only the list * *__all__ = ['foo', 'bar']
> * * * foo.py * * * * * # defines the function in_foo()
> * * * bar.py * * * * * # defines the function in_bar()
>
> In sample.py, I have this command at the top:
>
> from sub_one import *
>
> What am I doing wrong?


The statement `from sub_one import *` imports from sub_one/
__init__.py, which only imports the two modules into its namespace,
not their contents. What you need to do is bring into __init__.py
everything you want the star-import to pull into your code:

__init__.py:
from foo import *
from bar import *

foo.py:
__all__ = [ 'in_foo' ]
def in_foo():
...

bar.py:
__all__ = [ 'in_bar' ]
def in_bar():
...

If you structure is like this, you can restrict which items can be
imported within the defining file. If it doesn't make sense to do it
there, remove __all__ and just import directly in the __init__.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-06-2012
On Wed, 05 Dec 2012 20:58:46 -0800, Matt wrote:

> I have a directory structure that looks like this:
>
> sample.py
> sub_one/
> __init__.py # defines only the list __all__ = ['foo', 'bar']
> foo.py # defines the function in_foo()
> bar.py # defines the function in_bar()
>
> In sample.py, I have this command at the top:
>
> from sub_one import *
>
> I can't refer to in_foo() and in_bar() without prefacing them with the
> module names. I.e. foo.in_foo() and bar.in_bar() work, but I want to
> import them in the __main__ namespace of sample.py and refer to them as
> just in_foo() and in_bar().


Module `sub_one` has two public names, "foo" and "bar", exactly as you
say. So when you import * from it, you only get two names. Now, you could
do any of these inside sample.py:

# 1
from sub_one.foo import in_foo
from sub_one.bar import in_bar


# 2
from sub_one import *
in_foo = foo.in_foo
in_bar = bar.in_foo


Or you could turn to sub_one.__init__ and do this:

# 3
__all__ = ['in_foo', 'in_bar']
from foo import in_foo
from bar import in_bar


or any combination of the above.



--
Steven
 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      12-06-2012
It works now. Steven and Alex, thanks for your help!

I ended up leaving sample.py and foo.py and bar.p the way they were, and in __init__.py putting:

from foo import *
from bar import *

So my mistake was not importing the foo and bar modules into sub_one/__init__.py.

I also see how the __all__ array helps me control what gets imported. I can leave it out of __init__.py, and everything gets imported. So my three lessons are:

1) "from X import *" will look for an __all__ list in module X, or in __init__.py if X is a package instead of a module, and import only what is in that list. Module names are different than function names in that list.
2) if __all__ is not defined, "from X import *' will import everything in X's namespace
3) __init__.py acts like just another module, so you have to import the package contents that you want into it before you import the package into your code

Thanks again for the help!
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      12-06-2012
On 12/6/2012 11:50 AM, Matt wrote:
> It works now. Steven and Alex, thanks for your help!
>
> I ended up leaving sample.py and foo.py and bar.p the way they were, and in __init__.py putting:
>
> from foo import *
> from bar import *
>
> So my mistake was not importing the foo and bar modules into sub_one/__init__.py.
>
> I also see how the __all__ array helps me control what gets imported. I can leave it out of __init__.py, and everything gets imported. So my three lessons are:
>
> 1) "from X import *" will look for an __all__ list in module X, or in __init__.py if X is a package instead of a module, and import only what is in that list. Module names are different than function names in that list.
> 2) if __all__ is not defined, "from X import *' will import everything in X's namespace


.... that does not have a leading underscore. This is why there are
things like

import sys as _sys
from itertools import chain as _chain

in the stdlib when the module author does not define __all__.

> 3) __init__.py acts like just another module, so you have to import the package contents that you want into it before you import the package into your code



--
Terry Jan Reedy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
importing for package Javabean Java 4 03-02-2006 12:05 AM
Importing Package through Jar in JCreator Parameter Names problem Alex Java 0 02-20-2006 05:19 AM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Importing a package and looping through modules in the package Dave Python 2 02-10-2004 08:14 PM
importing classs without package in JSP ? mhk Java 1 11-28-2003 11:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57