By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Apr 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
May 1 '07 #2

P: n/a
En Mon, 30 Apr 2007 20:19:54 -0300, kwatch <kw****@gmail.comescribió:
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:

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

But, why do you want to do this exactly?

--
Gabriel Genellina

May 1 '07 #3

P: n/a
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" <gagsl-...@yahoo.com.arwrote:
En Mon, 30 Apr 2007 20:19:54 -0300,kwatch<kwa...@gmail.comescribió:
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:

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

But, why do you want to do this exactly?

--
Gabriel Genellina

May 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.