GNU/Linux >> Belajar Linux >  >> Linux

Bagaimana cara menginstal dan mengimpor modul Python saat runtime?

Anda dapat mengimpor pip daripada menggunakan subproses:

import pip

def install(package):
    pip.main(['install', package])

# Example
if __name__ == '__main__':
    try:
        import pexpect
    except ImportError:
        install('pexpect')
        import pexpect

Pengambilan lain:

import pip

def import_with_auto_install(package):
    try:
        return __import__(package)
    except ImportError:
        pip.main(['install', package])
    return __import__(package)

# Example
if __name__ == '__main__':
    pexpect = import_with_auto_install('pexpect')
    print(pexpect)

[sunting]

Anda harus mempertimbangkan untuk menggunakan requirements.txt bersama dengan pip. Sepertinya Anda mencoba mengotomatiskan penerapan (dan ini bagus!), di sabuk alat saya, saya juga memiliki virtualenvwrapper, gelandangan, dan mungkin.

Ini adalah output untuk saya:

(test)[email protected]:~/test# pip uninstall pexpect
Uninstalling pexpect:
  /usr/lib/python-environments/test/lib/python2.6/site-packages/ANSI.py
  /usr/lib/python-environments/test/lib/python2.6/site-packages/ANSI.pyc
  /usr/lib/python-environments/test/lib/python2.6/site-packages/FSM.py
  /usr/lib/python-environments/test/lib/python2.6/site-packages/FSM.pyc
  /usr/lib/python-environments/test/lib/python2.6/site-packages/fdpexpect.py
  /usr/lib/python-environments/test/lib/python2.6/site-packages/fdpexpect.pyc
  /usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect-2.4-py2.6.egg-info
  /usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect.py
  /usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect.pyc
  /usr/lib/python-environments/test/lib/python2.6/site-packages/pxssh.py
  /usr/lib/python-environments/test/lib/python2.6/site-packages/pxssh.pyc
  /usr/lib/python-environments/test/lib/python2.6/site-packages/screen.py
  /usr/lib/python-environments/test/lib/python2.6/site-packages/screen.pyc
Proceed (y/n)? y
  Successfully uninstalled pexpect
(test)[email protected]:~/test# python test.py
Downloading/unpacking pexpect
  Downloading pexpect-2.4.tar.gz (113Kb): 113Kb downloaded
  Running setup.py egg_info for package pexpect
Installing collected packages: pexpect
  Running setup.py install for pexpect
Successfully installed pexpect
Cleaning up...
<module 'pexpect' from '/usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect.pyc'>
(test)[email protected]:~/test#

Bagi yang menggunakan versi pip lebih besar dari 10.x, tidak ada main fungsi untuk pip jadi pendekatan alternatif menggunakan import pip._internal as pip bukannya import pip seperti :

Jawaban Paulo yang diperbarui

import pip._internal as pip

def install(package):
    pip.main(['install', package])

if __name__ == '__main__':
    try:
        import pexpect
    except ImportError:
        install('pexpect')
        import pexpect

Linux
  1. Cara Menginstal pip di Ubuntu 20.04

  2. Cara Menginstal dan Menggunakan PIP Python Package Manager di Rocky Linux 8

  3. Cara Menginstal Paket pip di CentOS/RHEL 7 dan 8

  1. Cara Menginstal Pip di CentOS 7

  2. Cara Menginstal PIP di Debian 9

  3. Cara Menginstal Pip di Mac

  1. Cara Menginstal Pip di CentOS 8

  2. Cara menginstal pip di RHEL 8 / CentOS 8

  3. Cara Menginstal Pip Python di Ubuntu 20.04