There are two kinds of import failure problems: one is to import self-written modules (i.e., files with the suffix.py), and the other is to import tripartite libraries. This article focuses on the second case, and we will discuss other related topics in more detail when opportunities arise.

The key to resolving the failure to import Python libraries is to either install the missing library in the runtime environment (note if it is a virtual one) or use an appropriate alternative. This problem can be divided into three cases:

Missing libraries in a single module

If we need to use a third-party library (such as Requests) while writing code, but are not sure whether the actual running environment has it installed, we can do this:

try:
    import requests
except ImportError:
    import os
    os.system('pip install requests')
    import requests
Copy the code

The effect is to install and import the Requests library if you can’t find it.

In some open source projects, we might also see something like this (using JSON as an example) :

ry:
    import simplejson as json
except ImportError:
    import json
Copy the code

The effect of this is to import the tripartite library SimpleJSON first, and if it cannot be found, use the built-in standard library JSON.

The advantage of this approach is that you don’t need to import additional libraries, but it has the disadvantage of ensuring that the two libraries are compatible in use. If you can’t find a replacement library in the standard library, it won’t work.

If you really can’t find a compatible standard library, you can write your own module (such as my_json.py), implement what you want, and then import it in an except statement.

ry:
    import simplejson as json
except ImportError:
    import my_json as json
Copy the code

Code word is not easy nonsense two sentences: there is a need to learn information or technical problems exchange “click”

Ii. Missing libraries in the whole project

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

PIP install for every possible missing tripartite library in your code is not an option;

2. What if a third-party library cannot be replaced by a standard library or a hand-written library?

3. What if the completed project is not allowed to make these modifications?

So the question here is: What about a project that wants to deploy to a new machine that involves a number of tripartite libraries, but none of them are pre-installed on the machine?

For a compliant project, by convention it usually contains a “requirements.txt” file that records all the dependent libraries for the project and their required version numbers. This is generated before the project is released using the command PIP freeze > requirements.txt.

Using the PIP install -r requirements. TXT command (run in the directory where the file is located, or write the path of the entire file in the command), all dependent libraries are automatically installed.

But what if the project isn’t compliant, or for some other unfortunate reason, we don’t have such documents?

A stupid method is to run the project, wait for it to go wrong, encounter a guide library failure, manually install one, then run the project again, encounter guide library failure install, and so on…

Automatically import any missing library

Is there a better way to automatically import missing libraries?

Is there a way to automatically import the required libraries without modifying the original code and without requiring the requirements. TXT file?

B: of course! First look at the results:

We take Tornado as an example. It can be seen from the first step that we have not installed Tornado. When Tornado is imported again after the second step, the program will automatically download and install Tornado for us, so no error will be reported.

Autoinstall is our hand-written module with the following code:

# The following code is verified in Python 3.6.1
import sys
import os
from importlib import import_module


class AutoInstall() :
    _loaded = set(a)    @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 None

sys.meta_path.append(AutoInstall)
Copy the code

This code uses sys.meta_path. Let’s print it out and see what it is.

  1. The import mechanism for Python 3 looks in the following order:

  2. Look in sys.modules, which caches all imported modules

  3. Look in sys.meta_path, which supports custom loaders

  4. Look in sys.path, which records the directory names of some libraries

  5. If not found, ImportError is thrown

Note that sys.meta_path varies between Python versions. For example, it differs significantly between Python 2 and Python 3. In newer Versions of Python 3 (3.4+), custom loaders need to implement the find_spec method, whereas earlier versions used find_module.

The above code is a custom class library loader, AutoInstall, can realize the purpose of automatic import third-party libraries. As a caveat, this method hijacks all newly imported libraries and breaks the original import method, so there may be some weird problems, too.

Sys. meta_path is an application of Python probes. Probes, or import hooks, are Python’s little-noticed mechanism, but it can do a lot of things, such as loading libraries on the network, modifying modules as they are imported, automatically installing missing libraries, uploading audit information, lazy loading, and more.

We won’t go into detail for lack of space. A final summary:

  1. You can use the try… Except way, to achieve a simple three-party library import or replacement

  2. When all missing dependency libraries are known (such as requirements.txt), you can install them manually

  3. With sys.meta_path, you can automatically import any missing libraries

As a Python developer, I spent three days to compile a set of Python learning tutorials, from the most basic Python scripts to Web development, crawlers, data analysis, data visualization, machine learning, etc. These materials can be “clicked” by the friends who want them