A single line of Python code dynamically loads dependencies

A few days ago I encountered a lot of user feedback in an open source project. The dependencies will not be installed, or PIP install -r requirements.txt will not respond.

There are many possible causes, and it is troublesome to investigate one by one.

To solve this problem once and for all, we usually go to site-packages and export the required packages to the project root directory.

But this is ultimately too crude for Python’s elegant personality.

So I wondered if I could dynamically import packages, and if not, call PIP download. In the end, I almost got what I wanted.

I have looked it up, and no one seems to have used this program before. It is very convenient for me to use it myself, so I would like to share it with you.

Although I wanted to call it library for everyone to download, later I realized that this also depends on PIP, which violates the original intention of doing dynamic dependency

So MY recommendation is to use the quickstart – inject code running approach

Quick start

Kneel by Star Github-LouisYoungx/Dypend

throughpipInstallation operation

Download the dypend dependency package from PyPI

pip install dypend
Copy the code

Generate the requirements.txt dependency file locally

pip freeze > requirements.txt
Copy the code

Introduce dypend at the top of the entry file for your project without changing any other code

import dypend
Copy the code

Dypend will check to see if all packages in your Python environment are in requiredings. TXT, and if not, dypend will call PIP to download.

Injection code run

Generate the requirements.txt dependency file locally

pip freeze > requirements.txt
Copy the code

Add the following code at the top of the entry file for your project without changing anything else

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(a)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 the code

Dypend will check to see if all packages in your Python environment are in requirements. TXT, and if not, dypend will download them automatically.