Hao poly Entertainment - ho poly Entertainment Sign -Python how to achieve

     Hao poly platforms specified registered download when writing Python projects, we may often encounter an error Import Module failure: ImportError: No module named 'xxx' or  ModuleNotFoundError: No module named 'xxx' 
  
      click on the link to register an account poly-ho -ho poly registration YL    -ho poly YL Login                                        
  

When writing Python projects, we may often encounter an error Import Module failure: ImportError: No module named 'xxx' or  ModuleNotFoundError: No module named 'xxx' .

Import failures, usually divided into two types: one is to import the module to write their own (ie as a suffix .py file), and the other is import-party libraries. This article focuses on the second case, in the future have the opportunity, we discuss in detail other related topics.

Importing Python library failed to solve the problem, in fact, the key is on the operating environment are installed with missing libraries (note whether the virtual environment), or use an appropriate alternative. The issue is divided into three cases:

A single missing library module

In the preparation of the code, if we need to use a tripartite libraries (such as requests), but not sure of the actual operating environment is installed it, you can write:

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

Wrote the effect that, if requests can not find the library, on the first installation, and then import.

In some open source projects, we may also see the following lines (to json for example):

try:
    import simplejson as json
except ImportError:
    import json 

Wrote effect, priority tripartite import library simplejson, if not, then use the built-in standard library json.

The benefits of this writing there is no need to import additional libraries, but it has the disadvantage that the need to ensure that the use of two libraries are compatible, if no alternative library in the standard library, it is not feasible .

If you really can not find a compatible standard library, you can also write your own modules (such as my_json.py), to achieve what you want, and then import it except statement.

try:
    import simplejson as json
except ImportError:
    import my_json as json 

Second, the entire project missing libraries

The above idea is for the development of the project, but it has several disadvantages: 1, in the code for each tripartite library may be missing are pip install, is not desirable; 2, a tripartite standard library or libraries can not be yourself handwritten Librarian, how to do? 3, the project has been formed, these modifications are not allowed to do how to do?

So the question here is: have a project that you want to deploy to the new machine, it involves a lot of tripartite library, but they are not pre-installed on the machine, how to do?

For a project compliance, in accordance with the agreement, it will usually contain a "requirements.txt" documents, records of all the project dependencies and their version numbers required. This is before the project release, use the command pip freeze > requirements.txt generates.

Use the command pip install -r requirements.txt (executed in the file directory, or write the whole file in the command path), will automatically give all the dependent libraries installed.

However, if the project is not compliant, or for other reasons bad, we do not have such a document, should you do?

A stupid way is to put the project up and running, wait for it to go wrong, encounters a guide library fails, a manually loaded, then run again once the project encountered guide library failed to install it, and so on ...... (omitted here 1 ten thousand bad language) ......

Third, the automatic import any missing libraries

Is there a better method can be imported automatically missing libraries it?

Without modifying the original code, without the need for "requirements.txt" file, there is no way to automatically import library need it?

Of course there is! First look at the results:

We tornado, for example, the first operation can be seen, we have not been installed tornado, after the second operation, import tornado again, the program will help us to automatically download and install the tornado, so no error.

autoinstall our handwritten module code is as follows:

# 以下代码在 python 3.6.1 版本验证通过
import sys
import os
from importlib import import_module class 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 None sys.meta_path.append(AutoInstall) 

This code is used in sys.meta_path our first print to see if it is what?

Python import mechanism 3 is in the discovery process, substantially in the following order:

  • Find in sys.modules, which caches all imported modules
  • Find in sys.meta_path, and it supports custom loader
  • Find in sys.path, which recorded some of the name of the directory where the library
  • If not found, throw  ImportError an exception

其中要注意,sys.meta_path 在不同的 Python 版本中有所差异,比如它在 Python 2 与 Python 3 中差异很大;在较新的 Python 3 版本(3.4+)中,自定义的加载器需要实现find_spec 方法,而早期的版本用的则是find_module 。

以上代码是一个自定义的类库加载器 AutoInstall,可以实现自动导入三方库的目的。需要说明一下,这种方法会“劫持”所有新导入的库,破坏原有的导入方式,因此也可能出现一些奇奇怪怪的问题,敬请留意。

sys.meta_path 属于 Python 探针的一种运用。探针,即import hook,是 Python 几乎不受人关注的机制,但它可以做很多事,例如加载网络上的库、在导入模块时对模块进行修改、自动安装缺失库、上传审计信息、延迟加载等等。

限于篇幅,我们不再详细展开了。最后小结一下:

  • 可以用 try...except 方式,实现简单的三方库导入或者替换
  • 已知全部缺失的依赖库时(如 requirements.txt),可以手动安装
  • 利用 sys.meta_path,可以自动导入任意的缺失库

参考资料:

https://github.com/liuchang0812/slides/tree/master/pycon2015cn

http://blog.konghy.cn/2016/10/25/python-import-hook/

https://docs.python.org/3/library/sys.html#sys.meta_path

公众号【Python猫】, 本号连载优质的系列文章,有喵星哲学猫系列、Python进阶系列、好书推荐系列、技术写作、优质英文推荐与翻译等等,欢迎关注哦。

 

Guess you like

Origin www.cnblogs.com/dakunqq/p/11762619.html