Skip to main content

Load dependent libraries dynamically.

Project description

dypend Visits Version MIT License

dypend Load dependent libraries dynamically.

A few days ago, I encountered many users feedback in an open source project. The Problem is they can not install the dependencies, such as execute pip install -r requirements.txt but nothing happened.

There are many problems like the wrong config of env that can cause this result and it's troublesome to check them one by one.

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

it's crude, not elegant.

So I wanted to load packages dynamically. if package do not exist, use pip to download them.

I searched Google roughly, it seems no one have mention this method, and I feel it's convenient to use, so I share it.

Although dypend is packaged for everyone to download, however that it depends on pip, which is against the intention of doing dynamic dependencies.

So I recommend using the Quick Start - Run by injecting code approach

Quick start

Run by pip install

Download the dypend package from PyPI .

pip install dypend

Freeze requirements.txt file.

pip freeze > requirements.txt

import dypend at the top of the project's entry file, without changing any other code.

import dypend

dypend will check packages in requirements.txt is available or not in your Python environment, if not, dypend will call pip to download them.

Run by injecting code

Freeze requirements.txt file.

pip freeze >  requirements.txt

Add the following code to the top of the project's entry file, without changing 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

dypend will check packages in requirements.txt is available or not in your Python environment, if not, dypend will call pip to download them.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dypend-0.0.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dypend-0.0.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file dypend-0.0.1.tar.gz.

File metadata

  • Download URL: dypend-0.0.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1

File hashes

Hashes for dypend-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f0734bb8ca22bd5938c27531779ae710b520e3f953b2c23db90d8cdf1e5d70ef
MD5 87b3697eb09d092039c20b6c97c085d6
BLAKE2b-256 21cdd3f777bc7a95e42b59def230d4a0cf972188f01060e7acba82438a889b33

See more details on using hashes here.

File details

Details for the file dypend-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dypend-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1

File hashes

Hashes for dypend-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 adffbe4d61ef12545c102a560ed6d1ba8017f3883177821a7f2a41443641ad69
MD5 a65ea2c1a153f37258a8f70c8472987c
BLAKE2b-256 2bfb054a9016e0ff69dfb1bc23b3fcac78b0b4cb5c333256b1f35a334d3bd9dd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page