Last active
August 28, 2019 11:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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