Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:: [': 0.04; 'found,': 0.07; 'importerror:': 0.07; 'subject:missing': 0.07; 'suggestions,': 0.07; 'sys': 0.07; 'subject:modules': 0.09; 'throws': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'cause.': 0.16; 'galaxy': 0.16; 'importerror': 0.16; 'subject:install': 0.16; 'subject:make': 0.16; 'subject:program': 0.16; 'sudo': 0.16; 'wrote:': 0.18; 'module': 0.19; 'subject:] ': 0.20; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'install': 0.23; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'raised': 0.31; 'url:python': 0.33; '-----': 0.33; 'monday,': 0.33; 'skip:# 10': 0.33; 'could': 0.34; "can't": 0.35; 'december': 0.35; 'except': 0.35; 'case,': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'module.': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'thank': 0.38; 'url:library': 0.38; 'handle': 0.38; 'skip:& 20': 0.39; '12,': 0.39; 'bad': 0.39; 'sure': 0.39; 'url:mail': 0.40; 'tell': 0.60; 'you.': 0.62; 'making': 0.63; 'email addr:gmail.com': 0.63; 'information': 0.63; 'day.': 0.63; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'contact': 0.67; 'it!': 0.67; 'notice:': 0.67; 'person,': 0.68; 'privileged.': 0.69; 'disclose': 0.74; 'imp': 0.84; 'safer': 0.84; 'url:imp': 0.84; 'medium.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=YizkuaQyJEce/ZXa8fTOCHFCTL6oMPjIqCyDhPVY9f0=; b=0gczOgG3r/jPrjBlunuL4iDttVNkArcIwGFC4GSpBhmHAMrUoG3wIwQQw5M1FJUStV UcOQJ4Zn50vC3ey+gcSGvNuh1JxwVC9YbPG/i7WkqX4dMm2ysELFJ5pdbB13KwaWcgyr uJ6t5iUp6ZsN6BWvRrmyfNXBNIcUjCmD2np2kcRxlIAXqqZ/SBZHijzIj6jYr+YegToH Vt43G73WyotmoEBfX15Xeh897x879892otJUAZFOU5l2iEbILI91dfu1ovUO7yBLrZBZ vcl3BzUx1gVJHlwVMuIHh3sbhDd0ZTY5/zrUzk7dj4AtLFhX5GArJ2YYA3H7GKXw04gK tS5w== MIME-Version: 1.0 X-Received: by 10.152.1.193 with SMTP id 1mr15390283lao.45.1418389161250; Fri, 12 Dec 2014 04:59:21 -0800 (PST) In-Reply-To: <4a2a3a0c-19f1-4899-9b38-914b2bd09423@googlegroups.com> References: <0757295c-1dac-40e8-9ebb-2128a7d41e38@googlegroups.com> <4a2a3a0c-19f1-4899-9b38-914b2bd09423@googlegroups.com> Date: Fri, 12 Dec 2014 13:59:21 +0100 Subject: Re: [newbie] how to make program suggest to install missing modules From: Chris Warrick To: hugocoolens Content-Type: multipart/alternative; boundary=089e013c62ac1c182c050a047643 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 158 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418389164 news.xs4all.nl 2864 [2001:888:2000:d::a6]:48860 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82441 --089e013c62ac1c182c050a047643 Content-Type: text/plain; charset=UTF-8 On Dec 12, 2014 11:56 AM, "hugocoolens" wrote: > > 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: sohcahtoa82@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 This is bad practice. The user should install it themselves: sudo pip may interfere with virtualenv and OS package managers. Just tell the user what to install and call it a day. -- Chris Warrick Sent from my Galaxy S3. --089e013c62ac1c182c050a047643 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

On Dec 12, 2014 11:56 AM, "hugocoolens" <hugocoolens@gmail.com> wrote:
>
> 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 Pichav= ant wrote:
> > > ----- Original Message -----
> > > > From: sohcahto= a82@gmail.com
> > > > try:
> > > >=C2=A0 =C2=A0 =C2=A0import someModule
> > > > except ImportError:
> > > >=C2=A0 =C2=A0 =C2=A0print "Module is missing"<= br> > > > >=C2=A0 =C2=A0 =C2=A0# handle it!
> > > >
> > > > Just make sure to attempt to import it again after maki= ng 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 confident= ial and may also be privileged. If you are not the intended recipient, plea= se notify the sender immediately and do not disclose the contents to any ot= her 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 cau= se.
>
> 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:
> =C2=A0 =C2=A0 imp.find_module('rtlsdr')
> except ImportError:
> =C2=A0 =C2=A0 print('Module rtlsdr is missing')
> =C2=A0 =C2=A0 print("I'll try to install it")
> =C2=A0 =C2=A0 os.system('sudo pip install pyrtlsdr')
> =C2=A0 =C2=A0 try:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 imp.find_module('rtlsdr')
> =C2=A0 =C2=A0 except ImportError:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 sys.exit('Sorry could not install modu= le rtlsdr, contact your
> local Python-guru')
> import rtlsdr
> print('Module rtlsdr succesfully imported')
>
>
>
> --
> https= ://mail.python.org/mailman/listinfo/python-list

This is bad practice. The user should install it themselves:= sudo pip may interfere with virtualenv and OS package managers. Just tell = the user what to install and call it a day.

--
Chris Warrick <https://chriswarric= k.com/>
Sent from my Galaxy S3.

--089e013c62ac1c182c050a047643--