Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, sys, subprocess, importlib
- lib = input("Library name (eg googlesearch-python)?_") # some libraries use a differnt name with pip install
- name = input("Import name (eg import googlesearch) Enter if same as library name_") # Enter if same eg colorama/colorama
- if name == "": # User entered "" as same name
- name = lib # set to same as library name
- try:
- module = importlib.import_module(name) # try import using library name
- print("Import succeeded!") # if no error then success!
- except ImportError:
- print("Trying to import " + lib + " with sys.executable -m pip install " + lib + "...")
- subprocess.check_call([sys.executable, "-m", "pip", "install", lib]) # use python -m pip install <lib>
- finally:
- module = importlib.import_module(name)
- print("Looks like you got lucky!")
- input("Enter to quit, then try importing " + name + " into your project or the shell")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement