// you’re reading...

Development

Reloading dynamically loaded python modules

In working on a python project I came across the need to dynamically load a module function. Think of it as a plugin mechanism where you have a package space that looks like:

/main.py
/drivers/
/fakedriver.py
/anotherdriver.py
/moredriver.py

main.py spins up a loop and on each iteration is told which driver to use via a String that it retrieves. So, one call could require fakedriver.py and the next could require anotherdriver.py.

Easy enough, use dynamic imports:

def importName(modulename, name):
""" Import a named object from a module in the context of this function,
which means you should use fully qualified module paths.

Return None on failure.
"""
try:
module = __import__(modulename, globals(), locals(), [name])
except ImportError:
return None

return vars(module)[name]

Works fine but main.py is a long living process and one of the things it knows how to do is update its drivers. Normally you would just call ‘reload(module)’ but in this case the module has no handle, its just a string so reload() fails. The fix for me was to check and delete it from sys.modules.


ef importName(modulename, name):
""" Import a named object from a module in the context of this function,
which means you should use fully qualified module paths.

Return None on failure.
"""
try:
# Delete the module from cache on every load to keep it fresh
# We have to wrap this in a try, on first load the key does not exist and throws a KeyError
try:
del sys.modules[modulename]
except KeyError:
print "Error"
module = __import__(modulename, globals(), locals(), [name])
except ImportError:
return None

return vars(module)[name]

Python people probably see this as obvious but it took me 30 minutes to solve it so I post it here for posterity.

Buy Diabecon
Order Pletal
Buy Menosan
Order Arava
Buy Oxycontin
Order Trazodone
Acne-n-Pimple Cream
Order Calan
Order Brite
Buy Nolvadex
Order Avandia
Order Abana
Buy Ansaid
Cheap Pravachol
Purchase Prozac
Order Sorbitrate
Cheap Bupropion
Accutane
Order Xenacore
Order Proscar
Cheap Didronel
Purchase Rimonabant
Percocet
Cheap Lamisil
Buy Isordil
Purchase Sinequan
Cheap Proventil
Buy Relafen
Cheap Diakof
Buy Ambien
Order Prandin
Buy Zyprexa
Buy Cephalexin
Order Retin-A
Purchase Dostinex
Purchase Zantac
Order Cipro
Purchase Norco
Order Arimidex
Order Myambutol
Cheap Lasuna
Purchase Flomax
Buy Amaryl
Buy Aceon
Flexeril
Buy Ashwagandha
Cheap Ephedrine
Mevacor
Cheap Purim
Buy Adipex
Order Nimotop
Buy Acyclovir
Purchase Lioresal
Cheap Sinequan
Prozac
Prandin
Himcocid
Buy Flovent
Vitamin A
Order Gasex
Allegra
Buy Cardizem
Purchase Arimidex
Order Ashwagandha
Order Aciphex
Buy Pilex
Cheap Oxycontin
Order Lexapro
Purchase Mysoline
Buy Lisinopril
Cheap Flovent
Cheap Xanax
Purchase Augmentin
Buy Aricept
Buy Mycelex-G
Purchase Avandamet
Himcolin
Tulasi
Buy Vasotec
Order Isordil
Flovent
Purchase AyurSlim
Pamelor
Purchase Brite
Cheap Cipro
Urispas
Xanax
Purchase Zithromax
Cheap Omnicef
Female Sexual
Levlen
Order Mentat
Buy Seroquel
Purchase Elimite
Order Prednisone
Purchase Cytotec
Order Deltasone
Aldactone
Buy Plavix
Adipex
Ultram
Altace
Order Lamisil
Buy Clonazepam
Lotensin
Order Norvasc
Lynoral
Purchase Phentermine
Order Dilantin
Cheap Clonazepam
Cheap Plavix
Purchase Alprazolam
Buy Nicotinell
Order Menosan
Isoptin
Purchase Loprox
Buy Lopid
Endep
Starlix
Purchase Quibron-T
Purchase Zovirax
Purchase V-Gel
Cheap Confido
Professional Plasma
Topamax
Order Loxitane
Purchase Allegra
Cheap Chitosan
Cheap Lasix
Cheap Fosamax
Cheap Parlodel
Order Procardia
Purchase Naprosyn
Cheap Celebrex
Buy Topamax
Cheap Hytrin
Buy Geodon
Buy Dosages
Sumycin
Ativan
Buy Altace
Cheap Rumalaya
Purchase Himcospaz
Order Bactroban
Buy Clomid
Order Sinequan
Buy Levaquin
Purchase Cystone
Oxytrol
Purchase Xenacore
Cheap Oxytrol
Order Lioresal
Rythmol SR
Buy Karela
Zerit
Buy Effexor
Buy Exelon
Order Atacand
Darvocet
Cheap Prozac
Butalbital
Order Zyban
Buy Mentat
Abana
Purchase Cheap
Purchase Tenuate
Sustiva
Nonoxinol
Buy Trimox
Order Reosto
Cheap Flonase
Purchase Omnicef
Purchase Prednisone
Purchase Plendil
Buy Proventil
Cheap Differin
Cheap Serevent
Order Prometrium
Ephedrine
Cheap Gasex
Purchase Aldactone
Levitra
Order Phentrimine
Order Diabecon
Order Pilex
Cheap Zyloprim
Methocarbam
Purchase Celexa
Kamagra
Buy Lincocin
Order Inderal
Order Effexor
Fosamax
Cheap Flomax
Buy Oxytrol
Buy Himplasia
Buy Phentermine
Order Rumalaya
Buy Tulasi
Buy Revia
Order Cozaar
Order Lopressor
Cheap Diarex
Purchase Cyklokapron
Order Rimonabant
Order Leukeran
Order Codeine
Prednisone
Order Casodex
Purchase Hytrin
Famvir
Cheap Ambien
Buy Zetia
Purchase Lotrisone
Purchase Flonase
Buy Sinequan
Geriforte
Bonnisan
Cheapest Generic
Buy Mysoline
Buy Acticin
Buy Prinivil
Cheap Zovirax
Diazepam
Order Danazol
Cheap Purinethol
Buy Rimonabant
Purchase Azulfidine
Depakote
Cheap Levlen
Purchase Herbolax
Order Himplasia
Motrin
Femcare
Purchase Adderall
Order Xeloda
Buy Nizoral
Cheap Seroquel
Purchase Seroquel
Copegus
Flonase
Buy Shoot
Purchase Watson
Mobic
Purchase Accupril
Cordarone
Purchase Cozaar
Purchase Mycelex-G
Purchase Oxycontin
Cheap Amoxil
Order Tenuate
Risperdal
Touch-Up Kit
Purchase Mentax
Premarin
Order Adderall
Cheap Eurax
Purchase Flovent
Lexapro
Lariam
Glucophage
Buy Mentax
Order Oxycontin
Atacand
Buy Himcospaz
Cheap Xenacore
Order Actos
Order Prilosec
Buy Calan
Casodex
Order Sarafem
Purchase Lasix
Biaxin
Order Pamelor
Male Sexual
Order Zovirax
Cheap Desyrel
Epivir-HBV
Order Xanax
Cheap Superman
Cheap Shallaki
Order AyurSlim
Order Lamictal
Cheap Imitrex
Order Ephedrine
Purchase Geriforte
Cheap CLA
Menosan
Buy Valium
Buy Consultation
Zimulti
Order Lotensin
Clonazepam
Purchase Lisinopril
Purchase Cialis
Buy Maxaquin
Purinethol
Cheap Lioresal
Aceon
Buy Hytrin

Discussion

Comments are disallowed for this post.

Comments are closed.