Skip to content

Instantly share code, notes, and snippets.

@narwanimonish
Last active August 28, 2019 11:44
Show Gist options
  • Save narwanimonish/8f01635ef80039315628576863932f63 to your computer and use it in GitHub Desktop.
Save narwanimonish/8f01635ef80039315628576863932f63 to your computer and use it in GitHub Desktop.
Function to check if package exist then import or else install package & import
package = 'sentry-sdk'
package_module = package.replace('-', '_')
def import_package(package_name, module_name):
try:
__import__(module_name)
except ImportError:
import pip
if hasattr(pip, 'main'):
pip.main(['install', package_name])
else:
pip._internal.main(['install', package_name])
__import__(module_name)
import_package(package, package_module)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment