473,804 Members | 3,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the best way to do dynamic imports ?

Hi list and python gurus :-)

I'm playing with some mod_python and web development. And in me code I
need to do som dynamic imports.
Right now I just do a:

exec 'import '+some_modulena me

But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc)
And have to I check if the modul is already loaded?
Another thing is how to call my dynamic imported moduls.
Now I use exec (as with my modules), like this:

exec 'newclass = '+classname+'() '
newclass.somefu nction()

Again it seems to easy. Is there a better/proper way to do it?
Do anybody now a good howto or tutorial to this?
Many thanks and hope you all have a happy new year :-)

/marc
Dec 30 '07 #1
5 1531
On Dec 30, 8:24 am, marcroy.ol...@g mail.com wrote:
Hi list and python gurus :-)

I'm playing with some mod_python and web development. And in me code I
need to do som dynamic imports.
Right now I just do a:

exec 'import '+some_modulena me
The correct way to do this is use the __import__ function. It takes
the string name of the module you want to import and returns the
module.
new_mod = __import__(some _modulename)
>
But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc)
Well, it's generally frowned on to use exec and eval.
And have to I check if the modul is already loaded?
sys.modules is a list of all imported modules, but python won't import
a module if it's already been loaded.
>
Another thing is how to call my dynamic imported moduls.
Now I use exec (as with my modules), like this:

exec 'newclass = '+classname+'() '
newclass.somefu nction()

Again it seems to easy. Is there a better/proper way to do it?
If you just have the string name of a class, you have to use eval or
exec:
newclass = eval(classname)

However, if you have the class object, you can just instantiate that:
class LargeClass:
def meth(): pass
some_class = LargeClass
new_class = some_class()
some_class.meth ()
>
Do anybody now a good howto or tutorial to this?

Many thanks and hope you all have a happy new year :-)
You, too!
>
/marc
Dec 30 '07 #2
Hallchen!

ma***********@g mail.com writes:
I'm playing with some mod_python and web development. And in me
code I need to do som dynamic imports. Right now I just do a:

exec 'import '+some_modulena me

But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc) And have to I check if the modul is
already loaded?
I use the imp module for this:

try:
file, pathname, description = imp.find_module (full_name)
my_module = imp.load_module (full_name, file, pathname, description)
finally:
file.close()

Tsch,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber. org
(See http://ime.webhop.org for further contact info.)
Dec 30 '07 #3
En Sun, 30 Dec 2007 12:24:53 -0200, <ma***********@ gmail.comescrib i�:
I'm playing with some mod_python and web development. And in me code I
need to do som dynamic imports.
Right now I just do a:

exec 'import '+some_modulena me

But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc)
Use __import__, specially if some_modulename comes from the outside.
What if some_modulename contains "modulename\nso me_nasty_functi on_call()"
And have to I check if the modul is already loaded?
Not needed; the standard import machinery already does that.
Another thing is how to call my dynamic imported moduls.
Now I use exec (as with my modules), like this:

exec 'newclass = '+classname+'() '
newclass.somefu nction()

Again it seems to easy. Is there a better/proper way to do it?
Use getattr to obtain the desired class from the containing module, then
use it as any other class:
the_module = __import__(some _modulename)
the_class = getattr(the_mod ule, classname)
o = the_class()
o.somefunction( )

Never use exec/eval and friends - and never ever use them in a web
application!
Do anybody now a good howto or tutorial to this?
No... what do you want covered?
Many thanks and hope you all have a happy new year :-)
Thanks, and a happy new year for you too!

--
Gabriel Genellina

Dec 30 '07 #4
First of thanks to all for you, especially for the quick replys.

Just need to walk the dog then I giv it a short.
On Dec 30, 3:57*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>
Do anybody now a good howto or tutorial to this?

No... what do you want covered?
Nothing, think you reply coved it all.

Otherwise I will be back ;-)

Dec 30 '07 #5
ma***********@g mail.com schrieb:
First of thanks to all for you, especially for the quick replys.

Just need to walk the dog then I giv it a short.
Please, don't kill your dog! We're a peace-loving community here that
respects dogs, and snakes and even trolls.

SCNR,

Diez
Dec 30 '07 #6

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

Similar topics

7
2248
by: Matthew Wilson | last post by:
Hi- I'm writing a bunch of classes, several of which need functions and variables defined in the math module. In some instances, I'm going to import my module like this: import myshapes and then in others, I'll do:
3
281
by: Rudy | last post by:
I'm building a application that will interface with SQL server. The amount of data is not alot, but the data will need to be proccessed quickly at about 3 - 4 minutes at a time. Then Idle for 2 minutes, then procces data again for about 3-4 minutes. The majority of the process will be client computers enter data into the tables, and that data getting calculated in a formula, and spitting out the return to client computer. This will be set...
6
2582
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
6
4482
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are clicked. I have read several posts which say to put your column generating section in the Page_Init section and it will solve the problem....however, it hasn't solved mine. Can somebody please take a look at this and provide any insight if possible?
7
1441
by: Stan Sainte-Rose | last post by:
Hi, I use this bit of code to generate dynamic tables in the page load section .... Dim ntable as New Table For i = 1993 To 2008 ntable = New Table ntable.ID = "Q" + i.ToString .... ....
4
2095
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event. Each question is displayed and answered, then result written to a SQL table. Then the next question is read from a table and displayed using the load_page event again. The questions display and function perfectly. The user anwers the question...
1
2886
by: JMann101 | last post by:
I am writing a ASP.NET(VB) application and am having some trouble. I have a datagrid which list users and when an admin clicks "edit" a defined column becomes visible and a dynamic listbox control is added to that column/row. When the admin clicks "update" I am unable to retrieve the values that were selected from that listbox. Basically, This is a multi-select listbox, so I need to loop to pull all selected items. This is the only...
669
26256
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
4
2050
by: vamshiv | last post by:
hi...im working on asp.net with VB i have created a database which hav fields id,name,image path in web form i hav written code for creation of image buttons dynamically i hav connected the form to DB and got the table into DataSet and also i hav placed one panel which contains image and label controls for dynamic image buttons i hav given the image URL from table only. and i hav written addHandler method to hanling events But for...
0
10320
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
10308
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
10073
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...
1
7609
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
6846
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
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
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
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.