Change Python / PIP to automatically install modules when import is not possible

Is there a way to change python / pip when import is not performed at runtime , it will try to install the module (with the same name) from pip and then import the module?

I would say that it would be better by default than just throwing an error. If there are any problems after loading the module from pip, then it also gives an error similar to when I notice that I can’t import something, try pip install and then it will come to the exact same error message.

I know that we can use requirements.txt to bind the package, but I speak from the “client” (the person using the script), and not the “provider” (the person providing the script); that is, as a client, I would like to be able to import any script and automatically resolve dependencies.

I understand that this can cause problems, but whenever I see ImportError, I just try to pip install module anyway. Only if the module will not work after installing the software "I will ask additional questions."

I thought of something like this snippet that will be “embedded” in the python process:

 def getDirectoryOfInterpreter(p): return "someway to return the directory" try: import somemodule except ImportError: os.system(getDirectoryOfInterpreter('THIS_INTERPRETER_BINARY') + ' pip install ' + "MODULE_NAME") import somemodule 
+7
python pip automation
source share
2 answers

You can do this with pipimport when using virtualenv . It probably works with system python if you have the appropriate permissions to write the necessary directories (at least site-packages , but your import may have some command that pip will try to put somewhere in PATH). Since it is good practice to always use virtualenvs for your own development, anyway, I never tried using pipimport with system python.

You need to import pipimport into your virtualenv manually:

 virtualenv venv source venv/bin/activate pip install pipimport 

Then create the autopipimport.py file that you import first in any module:

 # import and install pipimport import pipimport pipimport.install() 

Now in any other .py file you can do:

 import autopipimport import some.package.installable.by.pip 

I once tried (auto-curiosity) to add two related pipimport lines to venv/lib/python2.7/site.py to automatically "boot", but this was an early stage in the chain and didn't work.

pipimport will try to use pip to install a specific module once, storing information about what was tested in the .pipimport-ignore file (should be in venv , unless it is written).

+11
source share

Here is what I did for my curiosity.

Yes, this unobtrusively invokes a shell in the background with arbitrary user input using an unwanted function. Do not use this code for production.

 import os Libraries_Installed = False def Pip_Install(library): """Locate pip.exe on system and install library. Be sure to sanitize input to prevent RCE.""" global Lib_Installed if os.path.isfile('C:\\Python27\\Scripts\\pip.exe'): print '[!] c:\Python27\Scripts\pip.exe => pip' stream = os.popen('C:\Python27\Scripts\pip.exe install %s' % library) Result = stream.read() if 'Successfully installed' in str(Result): print '\n\n[+] Successfully installed %s' % library Lib_Installed = True elif 'FAIL' in str(Result).upper() or 'FAILED' in str(Result).upper() \ or 'FAILURE' in str(Result).upper(): print '[!] Unable to install %d' % library print '[!] Please manually run the pip installer' print '[!] try: C:\Python27\Scripts\pip.exe install %s' % library elif os.path.isfile('D:\\Python27\\Scripts\\pip.exe'): stream = os.popen('D:\Python27\Scripts\pip.exe install %s' % library) Result = stream.read() if 'Successfully installed' in str(Result): print '\n\n[+] Successfully installed %s' % library Lib_Installed = True elif 'FAIL' in str(Result).upper() or 'FAILED' in str(Result).upper() \ or 'FAILURE' in str(Result).upper(): print '[!] Unable to install %d' % library print '[!] Please manually run the pip installer' print '[!] try: C:\Python27\Scripts\pip.exe install %s' % library # Python library that may need to be installed try: import Tkinter import tkFileDialog except: print '[!] Error importing Tkinter ... Trying pip installer' Pip_Install('tkinter') if Lib_Installed == True: print '\n[!] The following Python libraries were installed automatically:' for library in Libs: print str(library) Q = raw_input('''\n[!] Please exit the program and run a new instance. [!] Enter [Y] to continue anyways''') if Q.upper()[0] == 'Y': pass else: exit() 
0
source share

All Articles