Does __import__ require a module to have a .py suffix?

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

    Does __import__ require a module to have a .py suffix?

    Hi all. I've got a python file called 'foo' (no extension). I want to
    be able to load it as a module, like so:

    m = __import__('foo ')

    However, the interpreter tells me "No module named foo". If I rename
    it foo.py, I can indeed import it. Is the extension required? Is there
    any way to override that requirement?

    Thanks,
    --Steve
  • Rick Dooling

    #2
    Re: Does __import__ require a module to have a .py suffix?

    On Mar 12, 11:22 am, mrstephengross <mrstevegr...@g mail.comwrote:
    Hi all. I've got a python file called 'foo' (no extension). I want to
    be able to load it as a module, like so:
    >
    m = __import__('foo ')
    >
    However, the interpreter tells me "No module named foo". If I rename
    it foo.py, I can indeed import it. Is the extension required? Is there
    any way to override that requirement?
    >
    I think you answered your own question, but if you want more info:

    From the Python Tutorial:



    "A module is a file containing Python definitions and statements. The
    file name is the module name with the suffix .py appended."

    RD



    Thanks,
    --Steve

    Comment

    • George Sakkis

      #3
      Re: Does __import__ require a module to have a .py suffix?

      On Mar 12, 12:22 pm, mrstephengross <mrstevegr...@g mail.comwrote:
      Hi all. I've got a python file called 'foo' (no extension). I want to
      be able to load it as a module, like so:
      >
      m = __import__('foo ')
      >
      However, the interpreter tells me "No module named foo". If I rename
      it foo.py, I can indeed import it. Is the extension required? Is there
      any way to override that requirement?
      You can use execfile:

      foo = {}
      execfile('foo', foo)

      Apart from the different syntax in accessing the module globals
      (attributes with __import__ (foo.x) vs dict entries with execfile
      (foo['x'])), there are probably more subtle differences but I can't
      tell for sure. It would be nice if someone more knowledgeable can
      compare and contrast these two appraches.

      George

      Comment

      • Gabriel Genellina

        #4
        Re: Does __import__ require a module to have a .py suffix?

        En Wed, 12 Mar 2008 18:02:54 -0200, Jean-Paul Calderone
        <exarkun@divmod .comescribió:
        On Wed, 12 Mar 2008 12:58:33 -0700 (PDT), George Sakkis
        <george.sakkis@ gmail.comwrote:
        >On Mar 12, 12:22 pm, mrstephengross <mrstevegr...@g mail.comwrote:
        >>
        >>Hi all. I've got a python file called 'foo' (no extension). I want to
        >>be able to load it as a module, like so:
        >>>
        >> m = __import__('foo ')
        >>>
        >You can use execfile:
        >>
        >foo = {}
        >execfile('foo' , foo)
        >>
        >Apart from the different syntax in accessing the module globals
        >(attributes with __import__ (foo.x) vs dict entries with execfile
        >(foo['x'])), there are probably more subtle differences but I can't
        >tell for sure. It would be nice if someone more knowledgeable can
        >compare and contrast these two appraches.
        >
        Another difference is that when you import a module, its code is
        (usually)
        only executed once. Each import after the first just returns a reference
        to the already-created module object. When you use execfile, the code is
        re-evaluated each time.
        The steps done by import are outlined in this message

        The relevant part (citing myself):

        newmodule = sys.modules[modulename] = ModuleType(modu lename)
        # constructor sets __name__ and a null __doc__
        newmodule.__bui ltins__ = current builtins
        newmodule.__fil e__ = filename
        code = read from filename and compile it
        exec code in newmodule.__dic t__

        Apart from sys.modules and __file__, there is another difference, the
        __builtins__ attribute. It is important: if not present, Python executes
        the code in "safe mode" where certain operations are disabled (and there
        is a big performance penalty).

        --
        Gabriel Genellina

        Comment

        Working...