Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __init__.py question

Reply
Thread Tools

__init__.py question

 
 
codecraig
Guest
Posts: n/a
 
      04-22-2005
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

 
Reply With Quote
 
 
 
 
gry@ll.mit.edu
Guest
Posts: n/a
 
      04-22-2005
from mynewdir import abc
abc.doFoo()

or

import mynewdir.abc
newdir.abc.doFoo()

 
Reply With Quote
 
 
 
 
Terry Hancock
Guest
Posts: n/a
 
      04-23-2005
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

 
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
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 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