473,788 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automatic import PEP


Hi,

I wrote the 'autoimp' module [1], which allows you to import lazy modules:

from autoimp import * (Import lazy wrapper objects around all modules; "lazy
modules" will turn into normal modules when an attribute
is first accessed with getattr()).
from autoimp import A, B (Import specific lazy module wrapper objects).

The main point of autoimp is to make usage of the interactive Python prompt
more productive by including "from autoimp import *" in the PYTHONSTARTUP file.
Thus one can use:
>>urllib2.urlop en('http://www.google.com' ).read()
Image.open('t est.bmp')
pylab.plot([1,2],[3,4])
scipy.linalg. eig([[1,2],[3,4]])
...
One can thenceforward use the interactive prompt without using the "import"
statement at all. In practice, I have found that there is little overhead in time
and memory for the initial import of all lazy modules (assuming all of the Python
module paths are on the local disk, and not on a network).

I have also found "autoimp" useful in writing normal Python applications; one can
use "from autoimp import *" at the top of a Python source file and thus have all
Python modules available to that source file, which results in increased
efficiency of writing the program with the downside that one has to think more
carefully about global variables to avoid name conflict bugs.

Now because this feature is useful, I thought of writing a PEP to add the
"lazy import" functionality to the Python interpreter.

Specifically:

lazily import *
lazily import A, B, C

This would involve the introduction of a new keyword, such as "lazily".

The PEP should include extensions to the builtin Python __import__ (__lazilyimport __ ?)
and the imp and zipimport modules so that the new "lazy import" feature can be
fully utilized by "dynamic" Python code.

Closely related to the "autoimp" module is Philip Eby's Importing module [2]. The Importing
module has the ability to lazily import a single module as well as a number of other features,
so Philip may wish to be involved, and features from Importing may be used in the PEP if
desired.

Opinions?

[1]. http://cheeseshop.python.org/pypi/autoimp/1.0.2
[2]. http://peak.telecommunity.com/DevCenter/Importing
_______________ _______________ _______________ _____
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Sep 23 '06
11 1853
On Sep 22, 10:09 pm, Connelly Barnes <connellybar... @yahoo.comwrote :
Hi,

I wrote the 'autoimp' module [1], which allows you to import lazy modules:

from autoimp import * (Import lazy wrapper objects around all modules; "lazy
modules" will turn into normal modules when an attribute
is first accessed with getattr()).
from autoimp import A, B (Import specific lazy module wrapper objects).

The main point of autoimp is to make usage of the interactive Python prompt
more productive by including "from autoimp import *" in the PYTHONSTARTUP file.
And it does. Gets rid of "oops, I forgot to import that module"
moments without cluttering my $PYTHONSTARTUP file with imports. +1.

My only complaint is that it breaks globals().

Oct 6 '06 #11
Dan Bishop wrote:
On Sep 22, 10:09 pm, Connelly Barnes <connellybar... @yahoo.comwrote :
>Hi,

I wrote the 'autoimp' module [1], which allows you to import lazy modules:

from autoimp import * (Import lazy wrapper objects around all modules; "lazy
modules" will turn into normal modules when an attribute
is first accessed with getattr()).
from autoimp import A, B (Import specific lazy module wrapper objects).

The main point of autoimp is to make usage of the interactive Python prompt
more productive by including "from autoimp import *" in the PYTHONSTARTUP file.

And it does. Gets rid of "oops, I forgot to import that module"
moments without cluttering my $PYTHONSTARTUP file with imports. +1.

My only complaint is that it breaks globals().
And startup takes quite long the first time, because a list of all available
modules must be gathered.

To work around that, one can either use a special importing "lib" object,
defined like that:

class _lib:
def __getattr__(sel f, name):
return __import__(name )
lib = _lib()
or modify the globals() to automatically look up modules on KeyError, like this
(put into PYTHONSTARTUP file):
import sys, code

class LazyImpDict(dic t):
def __getitem__(sel f, name):
try:
return dict.__getitem_ _(self, name)
except KeyError:
exc = sys.exc_info()
try:
return __import__(name )
except ImportError:
raise exc[0], exc[1], exc[2]

d = LazyImpDict()
code.interact(b anner='', local=d)
sys.exit()
Of course, this is not perfect as it may break quite a lot of things,
I haven't tested it thoroughly.

Georg
Oct 6 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3671
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
1
2113
by: praz | last post by:
Hi, I am trying to import a delimited text file using a macro. The problem is i am not able to generate a Primary key (Let access add primary key). I have tried saving the import specs using the import wizard and specifying the same in the macro, but when I run the macro, the text file is imported without the ID field (Primary key field). Please any help appreciated.
29
4229
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.Caching;
2
1429
by: Keith Jackson | last post by:
Does anybody know of a tool that will take a module as input, look for any wildcard imports, and then identify what symbols in the module come from which wildcard import? It could then expand out the from module import * to from module import foo, bar. It might need to ask the user on this, since they might want the wildcard import for something special, but it would still be *much* nicer then expanding the imports out by hand. ...
6
1992
by: dv | last post by:
Hi, I have the following question, I have an application which transfers a csv file (via FTP) to the server then on the server I have a PHP script which put the content of the CSV into a mysql The question is now how can I start without manual interaction this PHP file ?
2
1852
by: micklee74 | last post by:
hi is there something like an automatic debugger module available in python? Say if i enable this auto debugger, it is able to run thru the whole python program, print variable values at each point, or print calls to functions..etc...just like the pdb module, but now it's automatic. thanks
2
1501
by: leeroy881 | last post by:
Is there a way to automatically import incoming emails into a SQL Server Database? I know it is inefficient, but we have to run a number of rules on the email after it is received. Store it in a database and be able to connect other things, meetings, contacts, etc. Moving it back and forth in chunks can be difficult and the overhead could spiral out of control. So we were thinking of moving it to the DB as it was retrieved. What do you...
6
3473
by: Silfheed | last post by:
Heyas So we have the following situation: we have a testee.py that we want to automatically test out and verifiy that it is worthy of being deployed. We want our tester.py to test the code for testee.py without changing the code for testee.py. testee.py has a module in it that we want to mock in some tests and in others use the real module. /foo.py: (real module) class bar:
3
4654
by: myjish18 | last post by:
Hello, We have a DB2 UDB database v8.2.7 (db2 v8.2 fixpak 14) on AIX 5.3 which has Automatic Storage (AS) enabled. We want to disable automatic storage on entire database and/or disable automatic storage on all tablespaces. DB2 Manual says it once AS is enabled, it cant be changed. Is there any way to disable the AS or any other alternative? Please advice.
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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
9967
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
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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
3670
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.