current position:Home>I use one line of Python code to dynamically load dependencies

I use one line of Python code to dynamically load dependencies

2022-02-02 03:11:07 louisyoungx

Python One line code dynamic loading dependency

I met a lot of user feedback in an open source project a few days ago , No dependencies will be installed , Or perform pip install -r requirements.txt No response .

There are many possible causes , It's also troublesome to check one by one .

Want to solve this problem once and for all , Generally, everyone comes to site-packages Inside, guide out the required bags , Put it in the project root .

But it's too rough after all , Do not conform to the Python Elegant personality .

So I thought , Can you dynamically import packages , If not , Call again pip download . Finally, I almost realized my idea .

I probably checked , No one seems to have used this scheme now , It's very convenient for me to use , Share with you .

Although I want to make it library For everyone to download , Later, I thought that I had to rely on pip, Against the original intention of dynamic dependency

So I recommend using Quick start - Inject code to run Ways in

Quick start

adopt pip Installation and operation

stay PyPI download dypend Dependency package

pip install dypend
 Copy code 

Generate... Locally requirements.txt Dependency file

pip freeze > requirements.txt
 Copy code 

Introduce... At the top level of the entry file of the project dypend , Don't change any other code

import dypend
 Copy code 

At this time dypend Will check your Python Are there any in the environment requirements.txt In the package , without , dypend Would call pip download .

Inject code to run

Generate... Locally requirements.txt Dependency file

pip freeze > requirements.txt
 Copy code 

Add the following code at the top of the entry file of the project , Don't change any other code

import os
import re
REQUIREMENTS = os.getcwd() + '/requirements.txt'
def getDepends():
   requirements = open(REQUIREMENTS, 'r')
   libs = requirements.readlines()
   libList = []
   for lib in libs:
       try:
           name = re.search("^.+(?===)", lib).group(0)
           version = re.search("(?<===).+$", lib).group(0)
           libDict = {
               "name": name,
               "version": version
          }
           libList.append(libDict)
       except:
           continue
   return libList
def importLib():
   """Load python dependent libraries dynamically"""
​
   libList = getDepends()
​
   from pip._internal import main as pip_main
   import importlib
​
   def install(package):
       pip_main(['install', package])
​
   createVar = locals()
​
   for lib in libList:
       print(lib)
       try:
           createVar[lib["name"]] = importlib.import_module(lib["name"])
       except Exception as e:
           try:
               install(f'{lib["name"]}=={lib["version"]}')
               createVar[lib["name"]] = importlib.import_module(lib["name"])
           except Exception as e:
               print(e)
importLib
 Copy code 

At this time dypend Will check your Python Are there any in the environment requirements.txt In the package , without , You'll see depend Help you automatically download .

copyright notice
author[louisyoungx],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020311052058.html

Random recommended