So finally I got the answer: there was a "parent package" feature in the ni module but it was dropped of its awkwardness. This is not a big loss but this is exatly the feature that I need. Is there a person on this list who was against the "parent package" idea? He must know the answer or a workaround :-)

I just created a new module that can handle the 'from __.__.SomeModule import SomeClass' notation.
Keywords: Python import parent package
What is the right place to make it available for others?


  Laci 2.0

--
_________________________________________________________________
 Laszlo Nagy                  web: http://designasign.biz
 IT Consultant                mail: [EMAIL PROTECTED]

                Python forever!


#!/usr/bin/env python

"""Import hack module.

Importing this module replaces the __builtin__.__import__ function
(called by the import statement). After that you will be able to
import parent packages this way:

from __.__.SomeModule import SomeClass

where __.__ means a package that is one level above in the filesystem.
Please note that you can use the special '__.__' name only in the import
statement. The name '__.__' itself cannot be used to reference the 
parent package elsewhere.

NOTE: This syntax follows the convention introduced by the 'ni' package,
first appeared in Python 1.5.  The 'parent package' idea was dropped
because of its awkwardness. For details, please visit

http://www.python.org/doc/essays/packages.html

If you still feel that you need to use this feature, you can use this module.


LICENSE: Python license (http://www.python.org/psf/)

@author: <[EMAIL PROTECTED]>
@modified: 2005-04-08 15:42

"""

import __builtin__
import imp
import os

# Keep a reference to the original __import__ function
_orig_import = __builtin__.__import__

def __import__(name,globals,locals,fromlist):
    """Specialized import function that can import names from parent packages
    using this special form:
    
    from __.__.SomeModule import SomeClass
    
    Please note that you can use the special '__.__' name only in the import
    statement. The name '__.__' itself cannot be used to reference 
    the parent package elsewhere.
    """
    global _orig_import
    
    def _tail_module_path(path):
        """Returns the tail of a module path.split.
        
        If there is no tail (no dot in the module path) then raises an 
ImportError.
        
        Example:
            _tail_module_path('Lib.Db.PostgreSQL') will return 'Lib.Db'
            
        """
        index = path.rfind('.')
        if index <= 0:
            raise ImportError('Cannot import __parentpackage__ from a top level 
package.')
        return path[:index]
    #print "__import__ was called with name %s" % name
    if name.startswith(PARENTPACKAGE):
        #print "Trying to import %s" % name
        # Determine the file that needs to be imported
        package_fpath = globals['__file__']
        (dir,fname) = os.path.split(package_fpath)
        double_parent = not fname.startswith('__init__.')
        # Determine the new package name 
        package_ppath = globals['__name__']
        upper_package_ppath = _tail_module_path(package_ppath)
        if double_parent:
            upper_package_ppath = _tail_module_path(upper_package_ppath)
        #print "    Package name in the module where import was executed: %s" % 
package_ppath
        #print "    Package name for the parent package: %s" % 
upper_package_ppath
        name = name.replace(PARENTPACKAGE,upper_package_ppath)
    return _orig_import(name,globals,locals,fromlist)

# Install the new __import__ function
__builtin__.__import__ = __import__

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to