restricting import to current package

Fredrik Lundh effbot at telia.com
Sat Oct 7 12:19:51 EDT 2000


Jeff wrote:
> Is there a way to do something like:
>    from . import mymodule
> In other words:
>   try to import mymodule from the same package (or directory) as this module
>   If mymodule is not there, raise ImportError

how about:

    try:
        path = sys.path[:]
        sys.path = [os.path.dirname(__file__)]
        import mymodule
    finally:
        sys.path = path

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list