[Tutor] importing modules and packages

neubyr neubyr at gmail.com
Mon Oct 31 17:27:19 CET 2011


On Mon, Oct 31, 2011 at 4:38 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> neubyr wrote:
>>
>> Is it possible to tell whether import statement is importing a module
>> or package?  I am going through some example code with some import
>> statements - like 'import os, sys, time', 'import packed'. I know os,
>> sys and time are (built-in) modules and 'packed' is a package here .
>> But can I determine whether it's a package or module using any
>> command/method or by following some naming convention?
>
> Consider:
>
>>>> import curses  # A package.
>>>> curses.__file__
> '/usr/lib/python2.5/curses/__init__.pyc'
>>>> curses.__path__
> ['/usr/lib/python2.5/curses']
>
>
> Compare to:
>
>>>> import string  # A plain module.
>>>> string.__file__
> '/usr/lib/python2.5/string.pyc'
>>>> string.__path__
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute '__path__'
>
>
>
> Does this help?
>
>

That's helpful. Thanks a lot..

- neuby.r


More information about the Tutor mailing list