Import question

Costas Menico costas at meezon.com
Sun May 20 07:03:03 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote:


>...under what name?  If just undecorated 'myutil', this runs into
>potential conflicts.  What happens if your module a.py has:
>
>    import c:\\fora\\peep
>
>and your module b.py has
>
>    import c:\\forb\\peep
>
>WHICH one of the two modules, if either, becomes the one
>referred to by sys.modules['peep']?  The potential for deep
>ambiguity seems large to me, and Python's strategy when no
>obvious strategy exists to resolve the ambiguity is generally
>to NOT guess, but rather force explicit specification.
>
>An advanced Python programmer will easily build a custom
>import strategy for those exceedingly rare cases where the
>obvious approach of altering sys.path is not desired.  A less
>advanced user is best not encouraged to hardwire module
>paths in his or her source code, not a very good practice.
>

There is a solution for both issues you raise in my dream Python.

Assume there is a myfile.ini file and an ini file reader that allows
you to configure your system at installation time.

import ini 
aPath1 = ini.getIni('path1','myfile.ini')
aPath2 = ini.getIni('path2','myfile.ini')

import aPath1 + 'peep' as peep1
import aPath2 + 'peep' as peep2

Of course the syntax above is bogus since import does not accept an
expression for the module name. And the reload() would also work with
no changes.

Another advantage is the fact that you are specific about which module
to load. Without the path name you can have in your sys.path two
modules of the same name and un-intenionally load the wrong one. A
more insidious problem than hardwiring.

Costas

P.s. I don't believe hardwiring is the domain of "less advanced"
users. I do it all the time for throwaway code as I am sure you have.




More information about the Python-list mailing list