Silly import question (__file__ attribute)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mh

    Silly import question (__file__ attribute)

    So on most modules I import, I can access the .__file__ attribute to
    find the implementation. ie:[color=blue][color=green][color=darkred]
    >>> import time
    >>> time.__file__[/color][/color][/color]
    '/data1/virtualpython/lib/python2.3/lib-dynload/timemodule.so'[color=blue][color=green][color=darkred]
    >>> import socket
    >>> socket.__file__[/color][/color][/color]
    '/data1/virtualpython/lib/python2.3/socket.pyc'

    This doesn't work on the "thread" module:[color=blue][color=green][color=darkred]
    >>> import thread
    >>> thread.__file__[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'module' object has no attribute '__file__'

    A few questions. Why? Where is thread.py or thread.so? (I can't find
    it).

    thanks

    matt

  • Jack Diederich

    #2
    Re: Silly import question (__file__ attribute)

    On Thu, Mar 09, 2006 at 02:04:45PM -0800, mh wrote:[color=blue]
    > So on most modules I import, I can access the .__file__ attribute to
    > find the implementation. ie:[color=green][color=darkred]
    > >>> import time
    > >>> time.__file__[/color][/color]
    > '/data1/virtualpython/lib/python2.3/lib-dynload/timemodule.so'[color=green][color=darkred]
    > >>> import socket
    > >>> socket.__file__[/color][/color]
    > '/data1/virtualpython/lib/python2.3/socket.pyc'
    >
    > This doesn't work on the "thread" module:[color=green][color=darkred]
    > >>> import thread
    > >>> thread.__file__[/color][/color]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > AttributeError: 'module' object has no attribute '__file__'
    >
    > A few questions. Why? Where is thread.py or thread.so? (I can't find
    > it).
    >[/color]
    Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
    [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import sys
    >>> import thread
    >>> sys.modules['thread'][/color][/color][/color]
    <module 'thread' (built-in)>[color=blue][color=green][color=darkred]
    >>> thread[/color][/color][/color]
    <module 'thread' (built-in)>[color=blue][color=green][color=darkred]
    >>> ^D[/color][/color][/color]
    sprat:~# cd ~/src/python-head/Modules/
    sprat:~/src/python-head/Modules# ls thread*
    threadmodule.c threadmodule.o

    It is a built-in module so it doesn't have a .so (dll) or .py file
    to mention.

    -Jack

    Comment

    • Fredrik Lundh

      #3
      Re: Silly import question (__file__ attribute)

      "mh" wrote:
      [color=blue]
      > So on most modules I import, I can access the .__file__ attribute to
      > find the implementation. ie:[color=green][color=darkred]
      > >>> import time
      > >>> time.__file__[/color][/color]
      > '/data1/virtualpython/lib/python2.3/lib-dynload/timemodule.so'[color=green][color=darkred]
      > >>> import socket
      > >>> socket.__file__[/color][/color]
      > '/data1/virtualpython/lib/python2.3/socket.pyc'
      >
      > This doesn't work on the "thread" module:[color=green][color=darkred]
      > >>> import thread
      > >>> thread.__file__[/color][/color]
      > Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > AttributeError: 'module' object has no attribute '__file__'
      >
      > A few questions. Why? Where is thread.py or thread.so? (I can't find
      > it).[/color]
      [color=blue][color=green][color=darkred]
      >>> import sys
      >>> "thread" in sys.builtin_mod ule_names[/color][/color][/color]
      True

      </F>



      Comment

      • Steven D'Aprano

        #4
        Re: Silly import question (__file__ attribute)

        On Thu, 09 Mar 2006 17:25:20 -0500, Jack Diederich wrote:
        [color=blue]
        > It is a built-in module so it doesn't have a .so (dll) or .py file
        > to mention.[/color]

        Wouldn't it make sense for module.__file__ to be set to None rather than
        completely missing in this case?


        --
        Steven.

        Comment

        Working...