473,657 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 2002
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.c omescribió:
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.a rwrote:
En Mon, 30 Apr 2007 20:19:54 -0300,kwatch<kwa ...@gmail.comes cribió:
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
8921
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
2932
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 with the names of all your imports. What's the point of that? It gets worse when you want to do something like
7
14379
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 module. I have 2 questions. 1.. How do unload an imported module? 2.. how do I test for the existance of a method in a module without running it?
2
2001
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 module (given an instance of the first class) without importing it. Example of what I'm looking for: <<<file spam.py>>> class Spam(object):
30
2056
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 serialize pass
5
1538
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 can be assigned to a dnspython version, or to a test driver for in memory DNS. Or you can modify the source to "from drivermodule import DNSLookup". What is the friendliest way to make this configurable? Currently, users are modifying the...
12
1808
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 functions added to the global namespace. I would like to keep my own functions in a separate module and then import that module to the main script (that will be executed using the CLI interpreter). The problem is, I cannot access the functions in the...
2
2644
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
2153
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 that only means that the name is indeed available in the global scope.
0
8319
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.