Saturday 15 September 2012

linux - How to install and import Python modules at runtime? -


I want to set up a new Ubuntu installation to automatically write a script and install a demo-based app. Since the script will be run on a new server, the Python script needs to install some required modules automatically.

Here's the script.

  #! / Usr / bin / env dragon import subprocess import os import SYS def pip_install (mod): print subprocess.check_output ("PIP installation% s"% mod, shell = True) if __name__ == "__main__": if os.getuid ( )! = 0: Print "Sorry, you need to run the script as a root." Sys.exit () Try: Import except: pip_install ('pexpect') import pexpect # More code here ...   

pexpect installation is success though , The next line import PaxPete failed I think the code in the runtime does not contain information about the newly installed pexpect .

How to install and import the Python module on runtime? I am open to other methods. You can import POP instead of using subprocess:

Code> import pip def install: pip.main (['install', package]) # Example if __name__ == '__main__': try: Import except importError: Import Installation ('PepsiCo') Import :

take another:

  import dp import_with_auto_install (package): try: return __import __ (import) except importError: Pip.main (['install', package]) back __import__ (package) # example if __name__ == '__main__': pexpect = import_with_auto_inst All ('PaxPeat') Print (Pepsi)   

[edit]

You should consider experimenting with POP. It seems that you are trying to automate the deployment (and it is good!), In my device belt, I have virtualwevweeper and.

This is output to me:

  (test) root @ vendant: ~ / test # pip Uninstall Pax uninstalling PaxPreate: / usr / lib / python-environment / Test / lib / python2.6 / site-packages / ANSI.py / usr / lib / python-environment /test/lib/python2.6/site-packages/ANSI.pyc / usr / lib / python-environment / test / Lib / python2.6 / site-packages / FSM.py / usr / lib / python-environment / test /lib/python2.6/site-packages/FSM.pyc / usr / lib / python-environment / test / lib / Python2.6 / site-packages / fdpexpect.py / usr / lib / python- environment / test / lib /python2.6/site-packages/fdpexpect.pyc/ usr / lib / python-environment / test / lib / python2. 6 / site-packages / pexpect-2.4-py2.6.egg-info / usr / lib / python -investment / test / lib / python2.6 / site-packages / pexpect .py /usr/lib/python-environment/test/lib/python2.6/site-packages/pexpect.pyc/usr/lib/python- environment /test/lib/python2.6/site-packages/pxssh.py / Usr / lib / python-e environment / test / lib / python2.6 / site-packages / pxssh.pyc /usr/lib/python-environments/test/lib/python2.6/site-packages/screen.py / Usr / lib / python-environment / test / lib / python2.6 / site-packages / screen.pyc proceed (y / n)? Y successfully successfully uninstalled PaxPt (test) root @ vagrant: ~ / test # python test.py downloading / unpacking peptter Downloading PepsiC.4.4. tar.gz (113Kb): 113Kb package running setup.py egg_info for package Installing Packages: Setup.py to install pexpect installation Successfully installed PepsiCo cleaning ... ... '/usr/lib/python-environment/test/lib/python-en-virat-packages/pexpect. Pyc '& gt; From the module 'PepsiC' (test) Root @Wigget: ~ / test #    

No comments:

Post a Comment