[Q] module name available in 'from ... import ...' statement

K

kwatch

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
----------------------------------------
 
C

Carsten Haese

[...]
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
 
G

Gabriel Genellina

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?
 
K

kwatch

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 said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top