1

I'm writing a program for my grandpa. Occasionally an update will require a new module that's not installed on his computer. I don't want to have to manually install new modules every time, but he's not tech savvy enough to figure it out himself.

Is it OK for me to check for ImportErrors and install a module from the error message? If not, what's the alternative?

3
  • How are you distributing these files to your grandpa currently? How is he installing and/or running them? Commented Jul 1, 2015 at 19:51
  • 1
    @JaceBrowning I email him the script as I update it, and he puts it on his desktop and runs it.
    – spelchekr
    Commented Jul 1, 2015 at 22:56
  • 1
    @JaceBrowning And by running I mean double-clicking on the icon.
    – spelchekr
    Commented Jul 2, 2015 at 0:52

1 Answer 1

3

This is the job of the package manager. In the case of Python: pip.

If you package the library you are building and specify its dependencies, your users (grandpa), will simply need to run:

$ pip install --upgrade <name-of-package>

to get the latest version you've released and all of its dependencies. If you don't want to publish your code online, pip can also install from local sources:

$ pip install zipped_package_emailed_to_grandpa.zip

or, if you really need to package this in a single (Python 3.4+) script, you could call pip from within:

#!/usr/bin/env python3

def main():
   ...

if __name__ == '__main__':
    import sys, subprocess
    dependencies = ['dep_1', 'dep_2']
    subprocess.call([sys.executable, '-m', 'pip', 'install'] + dependencies)
    import dep_1, dep_2
    main()

but this is definitely uncommon.

5
  • See an example package here: github.com/jacebrowning/template-python-demo Commented Jul 1, 2015 at 18:56
  • It may be assuming too much to assume that a to a non-technical user, pip is something you run "simply".
    – user53141
    Commented Jul 1, 2015 at 19:41
  • Fair enough. I'll ask OP to clarify how this code is currently being distributed. Commented Jul 1, 2015 at 19:50
  • 1
    pip can read the package list from a file - pip install -r requirements.txt. The OP can distribute requirements.txt as part of their program, and when they add a new module requirement they add it to requirements.txt. The installation command can be wrapped in a shell script that the grandpa just need to double-click.
    – Idan Arye
    Commented Jul 1, 2015 at 19:56
  • @JaceBrowning I email him new versions as I code them. For instance, I send him an email with script_rev16.py attached. That's about all he can handle.
    – spelchekr
    Commented Jul 1, 2015 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.