Two import questions in Python 3.0

Scott David Daniels Scott.Daniels at Acm.Org
Sat Jan 24 12:51:53 EST 2009


Kay Schluehr wrote:
> On 24 Jan., 09:21, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>> If you run A.py as a script, it does not "know" it lives inside a package.
>> You must *import* A for it to become aware of the package.
>> Also, the directory containing the script comes earlier than PYTHONPATH
>> entries in sys.path -- so watch for that case too.
> Thanks, yes. I always make the same error thinking that a directory
> with the ritual __init__ file is actually a package ( as some kind of
> platonic entity ), something that is more obvious to me than it is to
> the runtime. The relative import semantics introduced with Python 2.5
> has made the error just visible that was hidden to me for about a
> decade. Shit.
Temper the language a bit.  You lose your effectiveness by some people
reading the color of your words, rather than their meaning in code.

By the way, if you run the script as:

     $ python -m package.A

You may get what you want

demo (language fixed up a bit, moe info printed):

in .../lib/site-packages:
     possible.py
     -------------
     print('import from top possible: %s' % __file__)

in .../lib/site-packages/package:
     possible.py
     -------------
     print('import from package.possible: %s' % __file__)

     __init__.py
     -------------
     print('package initialized: %s' % __file__)


     A.py
     ------
     print('A started: %s' % __file__)
     import possible
     print('A running: %s' % __file__)

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list