473,471 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

__init__.py question

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

Jul 19 '05 #1
2 1182
gry
from mynewdir import abc
abc.doFoo()

or

import mynewdir.abc
newdir.abc.doFoo()

Jul 19 '05 #2
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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: user | last post by:
I have gotten properties to respond correctly, but when I try to do it in __init__: class foo: def getter(self): return "hello" def __init__(self):
2
by: Jim Jewett | last post by:
Normally, I expect a subclass to act in a manner consistent with its Base classes. In particular, I don't expect to *lose* any functionality, unless that was the whole point of the subclass. ...
6
by: Steven Bethard | last post by:
So when I'm writing a class and I define an __init__ method, I sometimes haven't called object.__init__, e.g.: class C(object): def __init__(self, x): self.x = x instead of class...
1
by: Jeffrey Borkent | last post by:
Hi There, Please let me rephrase the problem as many people misunderstood what i was trying to ask. I know that the __init__ is the class constructor, that was not my question. under the...
1
by: yang | last post by:
I just a newbie of python Now I found that almost every program use Tkinter have this line class xxx(xxx): """xxxxx""" def __init__(self): """xxxxx""" Frame.__init__(self)
6
by: zelzel.zsu | last post by:
I am a new learner of Python Programming Language. Now. I am reading a book. In the section relating to module, I see an example. the directory tree looks like below: root\ system1\...
7
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple...
19
by: dickinsm | last post by:
Here's an example of a problem that I've recently come up against for the umpteenth time. It's not difficult to solve, but my previous solutions have never seemed quite right, so I'm writing to...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
25
by: Erik Lind | last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more online and can write simple modules, but I still don't get when __init__ needs to be used as opposed to creating a class...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.