On Monday, December 8, 2014 9:00:13 PM UTC+1, sohca...@gmail.com wrote:
> On Monday, December 8, 2014 10:46:47 AM UTC-8, Jean-Michel Pichavant wrote:
> > ----- Original Message -----
> > > From: sohcahto...@gmail.com
> > > try:
> > >     import someModule
> > > except ImportError:
> > >     print "Module is missing"
> > >     # handle it!
> > > 
> > > Just make sure to attempt to import it again after making the call to
> > > pip to install it.
> > 
> > Note that ImportError may be raised for other reasons than a missing module.
> > 
> > Check https://docs.python.org/2/library/imp.html and the imp.find_module, 
> > it could be a safer way to check for a missing module.
> > 
> > JM
> > 
> > 
> > -- IMPORTANT NOTICE: 
> > 
> > The contents of this email and any attachments are confidential and may 
> > also be privileged. If you are not the intended recipient, please notify 
> > the sender immediately and do not disclose the contents to any other 
> > person, use it for any purpose, or store or copy the information in any 
> > medium. Thank you.
> Good point.
> Of course, imp.find_module ALSO throws ImportError if the module can't be 
> found, but at least in that case, you'd know the exact cause.

Thanks for the suggestions, you can see here below what I came up with.
All suggestions/corrections welcome:

#!/usr/bin/env python
import imp
import os
import sys
try:
    imp.find_module('rtlsdr')
except ImportError:
    print('Module rtlsdr is missing')
    print("I'll try to install it")
    os.system('sudo pip install pyrtlsdr')
    try:
        imp.find_module('rtlsdr')
    except ImportError:
        sys.exit('Sorry could not install module rtlsdr, contact your
local Python-guru')
import rtlsdr
print('Module rtlsdr succesfully imported')



-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to