Fedora Linux Support Community & Resources Center
  #1  
Old 28th March 2011, 08:33 PM
IHeart SymLinks Offline
Registered User
 
Join Date: Jan 2011
Posts: 128
linuxfedorafirefox
Python importing modules in functions

If I have code in a function that imports modules inside a function is that bad? Does it reimport it every time the function is run, therefore making the code inefficient?

I have code for a function that tried to import a module only on Windows and if it cannot import it defines a function with the same name that does the same thing in Linux. (This way the function will run in Windows and Linux).
__________________
I♥SymLinks

Junior SysAdmin and Python Newbie
Fedora 15 64 Bit (Fedora 13 install upgraded to 14 with preupgrade, then 15 using --distro-sync)
KDE 4.6.5

Laptop: Fedora 13 upgraded to 15 via --distro-sync

Can someone pls help me with The wireless on my laptop?
Reply With Quote
  #2  
Old 29th March 2011, 11:11 PM
MacHala Offline
Registered User
 
Join Date: Jun 2009
Posts: 18
linuxfedorafirefox
Re: Python importing modules in functions

and what about:
Code:
try:
  functionname
except NameError:
  import module
  functionname = module.function
Reply With Quote
  #3  
Old 30th March 2011, 06:00 PM
IHeart SymLinks Offline
Registered User
 
Join Date: Jan 2011
Posts: 128
linuxfedorafirefox
Re: Python importing modules in functions

Would that be more efficient than trying to import the module each time?
__________________
I♥SymLinks

Junior SysAdmin and Python Newbie
Fedora 15 64 Bit (Fedora 13 install upgraded to 14 with preupgrade, then 15 using --distro-sync)
KDE 4.6.5

Laptop: Fedora 13 upgraded to 15 via --distro-sync

Can someone pls help me with The wireless on my laptop?
Reply With Quote
  #4  
Old 31st March 2011, 08:10 PM
MacHala Offline
Registered User
 
Join Date: Jun 2009
Posts: 18
linuxfedorafirefox
Re: Python importing modules in functions

i meant that you could use this code at the beggining of program, so you would be sure, you have that function available afterwards
Reply With Quote
  #5  
Old 31st March 2011, 10:50 PM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,050
solarisfirefox
Re: Python importing modules in functions

You should probably be asking why are you doing that in the first place. In python when you start needing to hide module imports like that it's probably a hint that the functions in question should be in another file or module of their own.
But what I believe python does is that each time it comes to an import it searches the sys.path or sys.modules lists to see if the import is there already. It only attempts to load the module if it's not found.
So the overhead is related to how many dirs are on the search path and how many other modules are imported.

see also:
http://stackoverflow.com/questions/1...op-of-a-module
http://stackoverflow.com/questions/4...789963#4789963

Last edited by marko; 31st March 2011 at 10:54 PM.
Reply With Quote
  #6  
Old 4th April 2011, 09:24 PM
IHeart SymLinks Offline
Registered User
 
Join Date: Jan 2011
Posts: 128
linuxfedorafirefox
Re: Python importing modules in functions

http://effbot.org/zone/import-confus...y-should-i-use

On that page that I was referred to in another one of my threads it says that it is correct that importing a module again has low overhead. When running a script on my machine it's probably not a big deal, but I wonder if on busy servers if little things like that can add up?

Perhaps if a module is only used in a certain function it would not be good to import it because if you never use the function you would have imported it for nothing?
__________________
I♥SymLinks

Junior SysAdmin and Python Newbie
Fedora 15 64 Bit (Fedora 13 install upgraded to 14 with preupgrade, then 15 using --distro-sync)
KDE 4.6.5

Laptop: Fedora 13 upgraded to 15 via --distro-sync

Can someone pls help me with The wireless on my laptop?
Reply With Quote
Reply

Tags
functions, importing, modules, python

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python high-precision for trig functions RupertPupkin Programming & Packaging 3 2nd September 2009 05:04 PM
python shell 'modules' command gtk error in help() linuxmaip Programming & Packaging 0 24th August 2009 09:08 PM
unable to list modules with Python help() Roel Using Fedora 1 18th January 2009 02:06 PM
there is no directory Build under /lib/modules/ /lib/modules/2.6.20-2925.9.fc7xen zhulizhong Installation, Upgrades and Live Media 4 2nd August 2007 06:27 AM
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Using Fedora 0 15th September 2005 05:53 PM


Current GMT-time: 10:03 (Sunday, 21-09-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat