FAQ
What is the condition of module name which is available in
'from .. import ..' statement ?

----------------------------------------
import os
print os.path # <module 'posixpath' from '/usr/local/
lib/python2.5/posixpath.pyc'>
from posixpath import sep # (no errors)
from os.path import sep # (no errors, wow!)
path = os.path
from path import sep # ImportError: No module named path
----------------------------------------

I found that library 'os.py' exists but 'os/path.py' doesn't exist
in '/usr/local/lib/python2.5'.
It means that file 'os/path.py' is not required to perform
'from os.path import sep' statement.

Could you teach me the condition of module name which is available
in 'from ... import ...' statement?

The goal what I want to do is to create a module by 'new' module
and specify that module name in 'from ...' statement.

----------------------------------------
# create a module
import new
foo = new.module('foo')
foo.pi = 3.14159
foo.x2 = lambda x: 2*x
# specify it in 'from' statement
from foo import pi, x2 # ImportError: No module named foo
----------------------------------------


--
regards,
kwatch

Search Discussions

  • Carsten Haese at May 1, 2007 at 12:41 am

    On Mon, 2007-04-30 at 16:19 -0700, kwatch wrote:
    [...]
    The goal what I want to do is to create a module by 'new' module
    and specify that module name in 'from ...' statement.

    ----------------------------------------
    # create a module
    import new
    foo = new.module('foo')
    foo.pi = 3.14159
    foo.x2 = lambda x: 2*x
    # specify it in 'from' statement
    from foo import pi, x2 # ImportError: No module named foo
    ----------------------------------------
    Not that this can't be done, but why do you think you have to create
    this 'foo' module on the fly? What is the actual problem you're trying
    to solve?

    -Carsten
  • Gabriel Genellina at May 1, 2007 at 1:02 am

    En Mon, 30 Apr 2007 20:19:54 -0300, kwatch <kwatch at gmail.com> escribi?:

    Could you teach me the condition of module name which is available
    in 'from ... import ...' statement?

    The goal what I want to do is to create a module by 'new' module
    and specify that module name in 'from ...' statement.
    You can create the module with imp.new_module, populate it, and then
    insert it inside sys.modules:

    py> from imp import *
    py> m = new_module("foo")
    py> m
    <module 'foo' (built-in)>
    py> m.a = 1
    py> def test():
    ... print "Function test inside foo module"
    ...
    py> m.test = test
    py> import sys
    py> sys.modules["foo"]=m
    py> m
    <module 'foo' (built-in)>
    py> foo
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'foo' is not defined
    py> import foo
    py> foo.test()
    Function test inside foo module

    But, why do you want to do this exactly?

    --
    Gabriel Genellina
  • Kwatch at May 1, 2007 at 2:42 am
    Thanks Carsten and Gabriel,
    the answer is 'sys.modules'.

    ----------------------------------------
    import sys, os
    from path import sep # ImportError: No module named path
    sys.modules['path'] = os.path
    from path import sep # (no error)
    ----------------------------------------

    I can now import from my module which is generated by 'new' module.

    ----------------------------------------
    # create a module
    import new
    foo = new.module('foo')
    foo.pi = 3.14159
    foo.x2 = lambda x: 2*x
    # register it to sys.modules
    import sys
    sys.modules['foo'] = foo
    # import from that module
    from foo import pi, x2
    ----------------------------------------

    Great.

    Not that this can't be done, but why do you think you have to create
    this 'foo' module on the fly? What is the actual problem you're trying
    to solve?
    I have a package which has several small modules and I want to
    integrate them into a file, with keeping compatibility.

    current:

    mylib/
    __init__.py
    html.py
    xhtml.py
    xml.py
    :
    :

    future:

    mylib.py


    --
    regards,
    kwatch


    "Gabriel Genellina" wrote:
    En Mon, 30 Apr 2007 20:19:54 -0300,kwatch<kwa... at gmail.com> escribi?:
    Could you teach me the condition of module name which is available
    in 'from ... import ...' statement?
    The goal what I want to do is to create a module by 'new' module
    and specify that module name in 'from ...' statement.
    You can create the module with imp.new_module, populate it, and then
    insert it inside sys.modules:

    py> from imp import *
    py> m = new_module("foo")
    py> m
    <module 'foo' (built-in)>
    py> m.a = 1
    py> def test():
    ... print "Function test inside foo module"
    ...
    py> m.test = test
    py> import sys
    py> sys.modules["foo"]=m
    py> m
    <module 'foo' (built-in)>
    py> foo
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'foo' is not defined
    py> import foo
    py> foo.test()
    Function test inside foo module

    But, why do you want to do this exactly?

    --
    Gabriel Genellina

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 30, '07 at 11:19p
activeMay 1, '07 at 2:42a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase