How to find the package a module belongs to ? 
Author Message
 How to find the package a module belongs to ?

I have a package containing several modules.
Is is possible, within a module, to say "give me the package I belong to" ?

Or, is there a way to access objects defined in __init__.py from a module
without explicitly referencing surounding package by name ?

-- Sasa



Sun, 16 Nov 2003 23:44:53 GMT  
 How to find the package a module belongs to ?
I don't fully understand your question, could you give
a code sample and show how it fails your expectation.

Quote:

> I have a package containing several modules.
> Is is possible, within a module,
> to say "give me the package I belong to" ?

We use a global variable in each module like this:
modname="mymodule"

And each function has a function name variable like this:
def myfunction():
    ftn="myfunction"

Then we can use the module and function name for debug and fatal errors:

def debug(ftn,txt):
    sys.stdout.write(string.join([modname,'.',ftn,':',txt,'\n'],''))
    sys.stdout.flush()

def fatal(ftn,txt):
    msg=string.join([modname,'.',ftn,':FATAL:',txt,'\n'],'')
    raise SystemExit, msg

Quote:
> Or, is there a way to access objects defined in __init__.py from
> a module without explicitly referencing surounding package by name ?

> -- Sasa

I think this is our solution to the problem that you raise.
It would sure be nice to pull this information from some
secret variable without having to explicitly name them.    

Thanks,
Jeff Sandys



Wed, 19 Nov 2003 03:48:23 GMT  
 How to find the package a module belongs to ?
...
[Sasa Zivkov]

Quote:
>> I have a package containing several modules.
>> Is is possible, within a module,
>> to say "give me the package I belong to" ?

Strictly speaking, no, not automatically, because module x within package p
may be loaded as p.x or just as x.

[Jeff]

Quote:
>It would sure be nice to pull this information from some
>secret variable without having to explicitly name them.    

But every module has a __file__ attribute that will tell you (whether the
module is loaded as x or p.x) that it came from some/path/p/x.py.

- Gordon



Wed, 19 Nov 2003 05:02:40 GMT  
 How to find the package a module belongs to ?

Quote:


> ...
> [Sasa Zivkov]
> >> I have a package containing several modules.
> >> Is is possible, within a module,
> >> to say "give me the package I belong to" ?

> Strictly speaking, no, not automatically, because module x within package p
> may be loaded as p.x or just as x.
> [Jeff]
> >It would sure be nice to pull this information from some
> >secret variable without having to explicitly name them.    

> But every module has a __file__ attribute that will tell you (whether the
> module is loaded as x or p.x) that it came from some/path/p/x.py.

Just use the module's __name__ attribute, in both cases it's 'p.x'.
A module is part of a package iff it's __name__ attribute contains a
'.'.

Quote:
>>> import xml.dom.html as H    
>>> H.__name__
'xml.dom.html'
>>> import socket as S
>>> S.__name__

'socket'

Regards
  Siggy

--

****** ceterum censeo javascriptum esse restrictam *******



Wed, 19 Nov 2003 05:57:21 GMT  
 How to find the package a module belongs to ?

Quote:



>> ...
>> [Sasa Zivkov]
>> >> I have a package containing several modules.
>> >> Is is possible, within a module,
>> >> to say "give me the package I belong to" ?

>> Strictly speaking, no, not automatically, because module x within
>> package p may be loaded as p.x or just as x.

[Siggy]

Quote:
>Just use the module's __name__ attribute, in both cases it's 'p.x'.
>A module is part of a package iff it's __name__ attribute contains a
>'.'.

>>>> import xml.dom.html as H    
>>>> H.__name__
>'xml.dom.html'
>>>> import socket as S
>>>> S.__name__
>'socket'

Given
  /test
    __init__.py
    a.py
    b.py

Then:
C:\tmp\test>python a.py
I'm b. My __name__ is b

C:\tmp\test>cd ..

C:\tmp>python test/a.py
I'm b. My __name__ is b

C:\tmp>python
python 1.5.42+ (#0, Mar 28 2000, 20:39:47) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam

Quote:
>>> from test import a

I'm b. My __name__ is test.b

ignoring-the-case-of-my-__name__-is-__main__ly y'rs

- Gordon



Wed, 19 Nov 2003 10:31:51 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Finding Tcllib Module Package dependeciy tree

2. Module Packages - package-search-strategy controversy

3. package.module != module

4. Puzzling difference between module and package.module

5. - finding outdated packages

6. Where does one find current logo packages?

7. Finding Unix freeware (was: Solaris packages of tcl/tk 8.0p2)

8. Package vrml.external.exception not found in import.

9. I can't find eai package

10. Package vrml.external.field not found in import

11. Where to find the package STD_1149_1_1990?

12. can't find package Tk?

 

 
Powered by phpBB® Forum Software