Frank Musterman
Frank Musterman

Reputation: 657

install a library if necessary

i wanted to send a short code i was proud of to a fellow student and realized he won't be able to run it since there is no reason for him to have installed the library. installing is of course super easy - but i realized this could happen often, mainly with beginners - wanted to build a simple function for it:

def smart_import(package_name):
    try:
        import package_name
    except ImportError:
        import pip
        pip.main(['install', package_name])

problem is that i dont really know how to pass in the name of the package as a value that could be called by import thought of converting a string back but that seems more complicated then i thought

Upvotes: 2

Views: 434

Answers (1)

wim
wim

Reputation: 362726

This is a bad idea for many reasons, the main one being that people generally don't expect a Python function call to automatically attempt to install software on their machine.

Here are some other problems this approach has:

  • The import name is not always corresponding to the distribution name. For example dateutil module is provided by python-dateutil distribution.
  • If you try to use smart_import from another module, it will leave the name imported into the wrong namespace.
  • Some distributions export multiple top-level imports, e.g. setuptools provides setuptools, easy_install, and pkg_resources.
  • In some cases, pip itself may not be installed.
  • The script might not have permission to install packages.
  • The user might want to install distributions into their homedir explicitly with --user, and your script itself can't know that.
  • The invocation of pip might need to change depending on whether or not you are within a virtualenv.
  • The installation might attempt to pull in dependencies which cause conflicts with other distributions a user has installed.
  • IDEs likely won't see that a module dynamically imported is there, and may squiggly underline their subsequent use as a name which can not be resolved.

What to do instead:

Package your code up into its own distribution. Write a setup.py which mentions the dependencies using the install_requires argument to in the setup call. Ask your fellow student to pip install mypackage, and the dependencies will be collected at install time rather than at runtime. You can send your distribution directly to them (as a mypackage-0.1.tar.gz file or as a wheel). Or you can upload it to an index such as the test pypi.

The Python Packaging User Guide is a beginner-friendly resource describing how to create and upload your own distribution.

Upvotes: 9

Related Questions