Advertisement
Inksaver

Import anything (Windows)

Jun 21st, 2023 (edited)
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | Source Code | 0 0
  1. import os, sys, subprocess, importlib
  2.  
  3. lib = input("Library name (eg googlesearch-python)?_")    # some libraries use a differnt name with pip install
  4. name = input("Import name (eg import googlesearch) Enter if same as library name_") # Enter if same eg colorama/colorama
  5.  
  6. if name == "": # User entered "" as same name
  7.     name = lib # set to same as library name
  8.  
  9. try:
  10.     module = importlib.import_module(name) # try import using library name
  11.     print("Import succeeded!")             # if no error then success!
  12.    
  13. except ImportError:
  14.     print("Trying to import " + lib + " with sys.executable -m pip install " + lib + "...")
  15.     subprocess.check_call([sys.executable, "-m", "pip", "install",  lib]) # use python -m pip install <lib>
  16.    
  17. finally:
  18.     module = importlib.import_module(name)
  19.     print("Looks like you got lucky!")
  20.  
  21. input("Enter to quit, then try importing " + name + " into your project or the shell")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement