473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
3 1978
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stefan Quandt | last post by:
I want to set a modules global variable from outside like this: <begin a.py> v = None def setv( x ): v = x def getv(): return v <end a.py> <begin b.py>
9
by: Paul Rubin | last post by:
That's what the Python style guides advise. They don't seem to like def frob(x): import re if re.search('sdfxyz', x): ... instead preferring that you pollute your module's global namespace...
7
by: Guy Robinson | last post by:
Hello, I have a directory of python scripts that all (should) contain a number of attributes and methods of the same name. I need to import each module, test for these items and unload the...
2
by: Reid Priedhorsky | last post by:
Dear group, I'd have a class defined in one module, which descends from another class defined in a different module. I'd like the superclass to be able to access objects defined in the first...
30
by: Franck PEREZ | last post by:
Hello, I'm developing a small XML marshaller and I'm facing an annoying issue. Here's some sample code: ########### My test application ############ class Foo(object): #The class I'd like to...
5
by: Stuart D. Gathman | last post by:
The pyspf package http://cheeseshop.python.org/pypi/pyspf/] can use either pydns, or dnspython. The pyspf module has a simple driver function, DNSLookup(), that defaults to the pydns version. It...
12
by: reubendb | last post by:
Hello, I am new to Python. I have the following question / problem. I have a visualization software with command-line interface (CLI), which essentially is a Python (v. 2.5) interpreter with...
2
by: krishna.000.k | last post by:
file1.py ---------- a = 20 from abc import * print "Should this be printed when 'a' is alone imported from this module" file2.py ---------- from file1 import a
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.