Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Python dynamic loading dependency with code

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use code to achieve Python dynamic loading dependency, many people may not understand, in order to make you better understand, the editor summarized the following, I hope you can get something according to this article.

A few days ago in an open source project encountered a lot of user feedback, will not install dependencies, or the implementation of pip install-r requirements.txt did not respond.

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

If you want to solve this problem once and for all, you usually go to site-packages to export the required packages and put them in the root directory of the project.

But after all, this is too rough to conform to Python's elegant personality.

So I wondered if I could introduce the package dynamically, and if not, call pip to download it. In the end, my vision almost came true.

I recommend using quick start-injecting code to run

Quick start

Kneel and beg for star GitHub-louisyoungx/dypend

Install and run through pip

Download the dypend dependency package at PyPI

Pip install dypend

Generate requirements.txt dependent files locally

Pip freeze > requirements.txt

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

Import dypend

At this point, dypend will check whether there are all packages in requirements.txt in your Python environment, and if not, dypend will call pip to download.

The injected code runs

Generate requirements.txt dependent files locally

Pip freeze > requirements.txt

Add the following code to the top of the entry file of the project without changing any other code

Import osimport reREQUIREMENTS = 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 ("(?

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report