search
HomeBackend DevelopmentPython TutorialLearn to automatically import missing libraries in python

python tutorial column introduces how to automatically import missing libraries.

Learn to automatically import missing libraries in python

Import failure problems are usually divided into two types: one is to import a module written by yourself (that is, a file with the suffix .py), The other is to import third-party libraries. This article mainly discusses the second situation. If we have the opportunity in the future, we will discuss other related topics in detail.

To solve the problem of failing to import the Python library, the key is actually to install the missing library in the running environment (note whether it is a virtual environment), or to use an appropriate alternative. This problem is divided into three situations:

1. Missing libraries in a single module

When writing code, if we need to use a third-party library (such as requests), but you are not sure whether it is installed in the actual running environment, then you can do this:

try:    import requestsexcept ImportError:    import os
    os.system('pip install requests')    import requests复制代码

The effect of writing this is that if the requests library cannot be found, install it first, Import again.

In some open source projects, we may also see the following writing (taking json as an example):

ry:    import simplejson as jsonexcept ImportError:    import json复制代码

The effect of writing this way is that priority is given to importing Third-party library simplejson, if not found, use the built-in standard library json.

The advantage of this way of writing is that there is no need to import additional libraries, but it has a disadvantage, that is, it needs to ensure that the two libraries are compatible in use. If an alternative library cannot be found in the standard library , then it is not feasible.

If you really can't find a compatible standard library, you can also write a module yourself (such as my_json.py) to achieve what you want, and then except statement to import it.

ry:    import simplejson as jsonexcept ImportError:    import my_json as json复制代码

Code words are not easy to talk nonsense. Two sentences: If you need learning materials or have technical questions, just "Click"

2. Missing libraries in the entire project

The above idea is for projects under development, but it has several shortcomings:

1. pip install for every third-party library that may be missing in the code , is not advisable;

2. What should I do if a third-party library cannot be replaced by the standard library or my own handwritten library?

3. What should I do if these modifications are not allowed for a completed project?

So the question here is: There is a project that I want to deploy to a new machine. It involves many third-party libraries, but none of them are pre-installed on the machine. What should I do?

For a compliant project, according to convention, it usually contains a "requirements.txt" file, which records all the dependent libraries of the project and their required version numbers. This is generated using the command pip freeze > requirements.txt before the project is released.

Use the command pip install -r requirements.txt (execute it in the directory where the file is located, or write the path of the entire file in the command) to automatically install all dependent libraries. superior.

But what if the project is non-compliant, or for some other unlucky reason we don’t have such a document?

A stupid way is to run the project, wait for it to error, and if an import library fails, install it manually, then run the project again, and if the import library fails, install it, and so on... …

3. Automatically import any missing libraries

Is there a better way to automatically import missing libraries?

Is there any way to automatically import the required libraries without modifying the original code and without the need for the "requirements.txt" file?

Of course! Let’s take a look at the effect first:

Learn to automatically import missing libraries in python

Let’s take tornado as an example. As you can see from the first step, we have not installed tornado##. #, after the second step, when importing tornado again, the program will automatically download and install tornado for us, so no errors will be reported.

autoinstall is our handwritten module, the code is as follows:

# 以下代码在 python 3.6.1 版本验证通过import sysimport osfrom importlib import import_moduleclass AutoInstall():
    _loaded = set()    @classmethod
    def find_spec(cls, name, path, target=None):
            if path is None and name not in cls._loaded:
                cls._loaded.add(name)
                print("Installing", name)                try:
                    result = os.system('pip install {}'.format(name))                    if result == 0:                        return import_module(name)                except Exception as e:
                    print("Failed", e)            return Nonesys.meta_path.append(AutoInstall)复制代码

sys.meta_path is used in this code, let’s print it first, See what it is?

Learn to automatically import missing libraries in python

  1. ##The

    import mechanism of Python 3 during the search process, the approximate sequence is as follows:

  2. Look in
  3. sys.modules

    , which caches all imported modules

  4. In
  5. sys.meta_path

    Search in, it supports custom loaders

  6. Search in
  7. sys.path

    , it records the directory names where some libraries are located

  8. If not found, throw
  9. ImportError

    Exception

It should be noted that sys.meta_path is different in different Python versions. For example, it is different between Python 2 and There is a big difference in Python 3; in newer Python 3 versions (3.4), custom loaders need to implement the find_spec method, while earlier The version used is find_module.

Learn to automatically import missing libraries in python

The above code is a custom class library loader AutoInstall, which can automatically import third-party libraries. It should be noted that this method will "hijack" all newly imported libraries and destroy the original import method, so some strange problems may also occur, so please pay attention.

sys.meta_path is an application of Python probe. Probes, or import hooks, are Python mechanisms that get almost no attention, but they can do a lot of things, such as loading libraries on the network and testing modules when importing them. Modify, automatically install missing libraries, upload audit information, delay loading, etc.

Limited by space limitations, we will not elaborate further. Finally, a summary:

  1. You can use try...except to implement simple third-party library import or replacement

  2. has been When you know all the missing dependent libraries (such as requirements.txt), you can manually install

  3. Use sys.meta_path to automatically import any The missing library

Related free learning recommendations:python tutorial(Video)

The above is the detailed content of Learn to automatically import missing libraries in python. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

For loop and while loop in Python: What are the advantages of each?For loop and while loop in Python: What are the advantages of each?May 13, 2025 am 12:01 AM

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.