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

Discussion in 'Python' started by mrstephengross, Mar 12, 2008.

  1. 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
     
    mrstephengross, Mar 12, 2008
    #1
    1. Advertising

  2. mrstephengross

    Rick Dooling Guest

    On Mar 12, 11:22 am, mrstephengross <> wrote:
    > 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:

    http://docs.python.org/tut/node8.html

    "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
     
    Rick Dooling, Mar 12, 2008
    #2
    1. Advertising

  3. mrstephengross

    John Krukoff Guest

    On Wed, 2008-03-12 at 09:22 -0700, mrstephengross wrote:
    > 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


    I recently solved a similar issue, importing from a string, with this
    code:

    >>> service = imp.new_module( 'ServiceModule' )
    >>> compiled = compile( '''...some code here...''', '<string>', 'exec',

    0, 1 )
    >>> exec compiled in service.__dict__


    You could probably shorten it for your needs by using execfile instead.
    If it's not in the current directory, you'll probably run into some
    issues with further imports not working as expected unless you set the
    names/paths right.

    --
    John Krukoff <>
    Land Title Guarantee Company
     
    John Krukoff, Mar 12, 2008
    #3
  4. On Mar 12, 12:22 pm, mrstephengross <> wrote:

    > 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
     
    George Sakkis, Mar 12, 2008
    #4
  5. En Wed, 12 Mar 2008 18:02:54 -0200, Jean-Paul Calderone
    <> escribió:

    > On Wed, 12 Mar 2008 12:58:33 -0700 (PDT), George Sakkis
    > <> wrote:
    >> On Mar 12, 12:22 pm, mrstephengross <> wrote:
    >>
    >>> 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
    http://groups.google.com/group/comp...adc39ac/3882ce35f13ff971#msg_96a590bca5f2be8c
    The relevant part (citing myself):

    newmodule = sys.modules[modulename] = ModuleType(modulename)
    # constructor sets __name__ and a null __doc__
    newmodule.__builtins__ = current builtins
    newmodule.__file__ = filename
    code = read from filename and compile it
    exec code in newmodule.__dict__

    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
     
    Gabriel Genellina, Mar 19, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco Herrn

    __import__() with packages

    Marco Herrn, Apr 4, 2004, in forum: Python
    Replies:
    5
    Views:
    481
    Hung Jung Lu
    Apr 6, 2004
  2. Coder Coder

    __import__

    Coder Coder, May 8, 2004, in forum: Python
    Replies:
    4
    Views:
    722
    Heiko Wundram
    May 8, 2004
  3. John Allman
    Replies:
    0
    Views:
    285
    John Allman
    Oct 16, 2006
  4. Phil
    Replies:
    2
    Views:
    470
  5. Zac Burns
    Replies:
    0
    Views:
    326
    Zac Burns
    Nov 13, 2009
Loading...

Share This Page