• <legend id='zHPFk'><style id='zHPFk'><dir id='zHPFk'><q id='zHPFk'></q></dir></style></legend>

    <small id='zHPFk'></small><noframes id='zHPFk'>

      <bdo id='zHPFk'></bdo><ul id='zHPFk'></ul>
  • <i id='zHPFk'><tr id='zHPFk'><dt id='zHPFk'><q id='zHPFk'><span id='zHPFk'><b id='zHPFk'><form id='zHPFk'><ins id='zHPFk'></ins><ul id='zHPFk'></ul><sub id='zHPFk'></sub></form><legend id='zHPFk'></legend><bdo id='zHPFk'><pre id='zHPFk'><center id='zHPFk'></center></pre></bdo></b><th id='zHPFk'></th></span></q></dt></tr></i><div id='zHPFk'><tfoot id='zHPFk'></tfoot><dl id='zHPFk'><fieldset id='zHPFk'></fieldset></dl></div>
        <tfoot id='zHPFk'></tfoot>
      1. 如何检查模块/库/包是否是 python 标准库的一部分

        时间:2023-09-13
          <tbody id='H86vq'></tbody>
        <legend id='H86vq'><style id='H86vq'><dir id='H86vq'><q id='H86vq'></q></dir></style></legend>

        <i id='H86vq'><tr id='H86vq'><dt id='H86vq'><q id='H86vq'><span id='H86vq'><b id='H86vq'><form id='H86vq'><ins id='H86vq'></ins><ul id='H86vq'></ul><sub id='H86vq'></sub></form><legend id='H86vq'></legend><bdo id='H86vq'><pre id='H86vq'><center id='H86vq'></center></pre></bdo></b><th id='H86vq'></th></span></q></dt></tr></i><div id='H86vq'><tfoot id='H86vq'></tfoot><dl id='H86vq'><fieldset id='H86vq'></fieldset></dl></div>

          <small id='H86vq'></small><noframes id='H86vq'>

            <bdo id='H86vq'></bdo><ul id='H86vq'></ul>
              1. <tfoot id='H86vq'></tfoot>

                • 本文介绍了如何检查模块/库/包是否是 python 标准库的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我已经用 pip 安装了很多库/模块/包,现在我无法区分哪些是 Python 标准库的原生库,哪些不是.当我的代码在我的机器上运行但在其他任何地方都不起作用时,这会导致问题.

                  I have installed sooo many libraries/modules/packages with pip and now I cannot differentiate which is native to the python standard library and which is not. This causes problem when my code works on my machine but it doesn't work anywhere else.

                  如何检查我在代码中导入的模块/库/包是否来自 python stdlib?

                  假设检查是在具有所有外部库/模块/包的机器上完成的,否则我可以简单地在没有它们的另一台机器上执行 try-except 导入.

                  Assume that the checking is done on the machine with all the external libraries/modules/packages, otherwise I could simply do a try-except import on the other machine that doesn't have them.

                  例如,我确信这些导入可以在我的机器上运行,但是当它在仅安装了普通 Python 的机器上时,它会中断:

                  For example, I am sure these imports work on my machine, but when it's on a machine with only a plain Python install, it breaks:

                  from bs4 import BeautifulSoup
                  import nltk
                  import PIL
                  import gensim
                  

                  推荐答案

                  您必须检查所有已导入的模块,看看其中是否有任何模块位于标准库之外.

                  You'd have to check all modules that have been imported to see if any of these are located outside of the standard library.

                  以下脚本不是防弹的,但应该为您提供一个起点:

                  The following script is not bulletproof but should give you a starting point:

                  import sys
                  import os
                  
                  external = set()
                  exempt = set()
                  paths = (os.path.abspath(p) for p in sys.path)
                  stdlib = {p for p in paths
                            if p.startswith((sys.prefix, sys.real_prefix)) 
                            and 'site-packages' not in p}
                  for name, module in sorted(sys.modules.items()):
                      if not module or name in sys.builtin_module_names or not hasattr(module, '__file__'):
                          # an import sentinel, built-in module or not a real module, really
                          exempt.add(name)
                          continue
                  
                      fname = module.__file__
                      if fname.endswith(('__init__.py', '__init__.pyc', '__init__.pyo')):
                          fname = os.path.dirname(fname)
                  
                      if os.path.dirname(fname) in stdlib:
                          # stdlib path, skip
                          exempt.add(name)
                          continue
                  
                      parts = name.split('.')
                      for i, part in enumerate(parts):
                          partial = '.'.join(parts[:i] + [part])
                          if partial in external or partial in exempt:
                              # already listed or exempted
                              break
                          if partial in sys.modules and sys.modules[partial]:
                              # just list the parent name and be done with it
                              external.add(partial)
                              break
                  
                  for name in external:
                      print name, sys.modules[name].__file__
                  

                  这是一个新模块,脚本中的所有导入之后导入它,它会打印它认为不是标准库一部分的所有模块.

                  Put this is a new module, import it after all imports in your script, and it'll print all modules that it thinks are not part of the standard library.

                  这篇关于如何检查模块/库/包是否是 python 标准库的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 setuptools 和 setup.py 从包中排除单个文件 下一篇:Pip 安装挂起

                  相关文章

                  最新文章

                  <legend id='nvC6c'><style id='nvC6c'><dir id='nvC6c'><q id='nvC6c'></q></dir></style></legend>

                    <tfoot id='nvC6c'></tfoot>

                      <i id='nvC6c'><tr id='nvC6c'><dt id='nvC6c'><q id='nvC6c'><span id='nvC6c'><b id='nvC6c'><form id='nvC6c'><ins id='nvC6c'></ins><ul id='nvC6c'></ul><sub id='nvC6c'></sub></form><legend id='nvC6c'></legend><bdo id='nvC6c'><pre id='nvC6c'><center id='nvC6c'></center></pre></bdo></b><th id='nvC6c'></th></span></q></dt></tr></i><div id='nvC6c'><tfoot id='nvC6c'></tfoot><dl id='nvC6c'><fieldset id='nvC6c'></fieldset></dl></div>
                    1. <small id='nvC6c'></small><noframes id='nvC6c'>

                        <bdo id='nvC6c'></bdo><ul id='nvC6c'></ul>