__init__.py question

Discussion in 'Python' started by codecraig, Apr 22, 2005.

  1. codecraig

    codecraig Guest

    Ok, I have the following directory structure

    C:\pycode
    --> blah.py
    --> mynewdir
    --> __init__.py
    --> abc.py

    [[ C:\pycode\mynewdir\abc.py ]]

    def doFoo():
    print "hi"

    def doBar():
    print "bye"

    [[ C:\pycode\mynewdir\__init__.py ]]

    from mynewdir import *

    [[ C:\pycode\blah.py ]]

    ????

    what do i import in blah.py so that I can accesss, abc.doFoo() ?

    thanks
     
    codecraig, Apr 22, 2005
    #1
    1. Advertising

  2. codecraig

    Guest

    from mynewdir import abc
    abc.doFoo()

    or

    import mynewdir.abc
    newdir.abc.doFoo()
     
    , Apr 22, 2005
    #2
    1. Advertising

  3. On Friday 22 April 2005 07:19 am, codecraig wrote:
    > Ok, I have the following directory structure
    >
    > C:\pycode
    > --> blah.py
    > --> mynewdir
    > --> __init__.py
    > --> abc.py
    >
    > [[ C:\pycode\mynewdir\abc.py ]]
    >
    > def doFoo():
    > print "hi"
    >
    > def doBar():
    > print "bye"
    >
    > [[ C:\pycode\mynewdir\__init__.py ]]
    >
    > from mynewdir import *


    This didn't work, did it? There is no module
    "mynewdir.py" nor a package "mynewdir" in
    the "mynewdir" directory, and I don't think import
    will search up to find the container.

    I suspect you meant that __init__.py says:

    from abc import *

    > [[ C:\pycode\blah.py ]]
    >
    > ????
    >
    > what do i import in blah.py so that I can accesss,

    abc.doFoo() ?

    Assuming the above, and that you want to access
    it as you have written it, that would be:

    from mynewdir import abc

    Note that in order to use this form, you don't have
    to have *anything* in mynewdir/__init__.py --- it can
    be an empty file, as long as it exists.

    You only need to use an import in __init__.py if you
    want it to automatically run when you import the
    package.

    E.g. if you did:

    import mynewdir

    You could access your function as:

    mynewdir.abc.doFoo

    (which requires the import statement in __init__.py).

    Cheers,
    Terry


    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
     
    Terry Hancock, Apr 23, 2005
    #3
    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. chenyu
    Replies:
    1
    Views:
    765
    Alex Martelli
    Nov 7, 2003
  2. Jeffrey Borkent

    def __init__ question in a class definition

    Jeffrey Borkent, Feb 7, 2005, in forum: Python
    Replies:
    4
    Views:
    317
    Steve Holden
    Feb 8, 2005
  3. Steven Bethard
    Replies:
    2
    Views:
    462
    Steven Bethard
    Feb 16, 2005
  4. Kent Johnson
    Replies:
    7
    Views:
    923
    Jan Niklas Fingerle
    Feb 12, 2006
  5. Ramchandra Apte
    Replies:
    17
    Views:
    350
    Manuel Pégourié-Gonnard
    Sep 30, 2012
Loading...

Share This Page