Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82137 > unrolled thread
| Started by | Jean-Michel Pichavant <jeanmichel@sequans.com> |
|---|---|
| First post | 2014-12-08 19:46 +0100 |
| Last post | 2014-12-12 13:59 +0100 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
Re: [newbie] how to make program suggest to install missing modules Jean-Michel Pichavant <jeanmichel@sequans.com> - 2014-12-08 19:46 +0100
Re: [newbie] how to make program suggest to install missing modules sohcahtoa82@gmail.com - 2014-12-08 11:59 -0800
Re: [newbie] how to make program suggest to install missing modules hugocoolens <hugocoolens@gmail.com> - 2014-12-12 02:52 -0800
Re: [newbie] how to make program suggest to install missing modules Chris Warrick <kwpolska@gmail.com> - 2014-12-12 13:59 +0100
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
|---|---|
| Date | 2014-12-08 19:46 +0100 |
| Subject | Re: [newbie] how to make program suggest to install missing modules |
| Message-ID | <mailman.16730.1418064395.18130.python-list@python.org> |
----- 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.
[toc] | [next] | [standalone]
| From | sohcahtoa82@gmail.com |
|---|---|
| Date | 2014-12-08 11:59 -0800 |
| Message-ID | <f46868d7-bce2-440b-a5e6-a9eea44aa62d@googlegroups.com> |
| In reply to | #82137 |
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.
[toc] | [prev] | [next] | [standalone]
| From | hugocoolens <hugocoolens@gmail.com> |
|---|---|
| Date | 2014-12-12 02:52 -0800 |
| Message-ID | <4a2a3a0c-19f1-4899-9b38-914b2bd09423@googlegroups.com> |
| In reply to | #82143 |
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')
[toc] | [prev] | [next] | [standalone]
| From | Chris Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2014-12-12 13:59 +0100 |
| Message-ID | <mailman.16907.1418389164.18130.python-list@python.org> |
| In reply to | #82435 |
[Multipart message — attachments visible in raw view] — view raw
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 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 <https://chriswarrick.com/>
Sent from my Galaxy S3.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web