2011-06-24 44 views
25

我想要的東西像sys.builtin_module_names除了標準庫。沒有工作,其他的事情:我如何獲得所有Python標準庫模塊的列表

  • sys.modules - 只顯示已加載
  • sys.prefix模塊 - 一個將包括非標準庫模塊編輯路徑:似乎並沒有給裏面的工作virtualenv中。

我想這個名單是爲了讓我可以將它傳遞給tracehttp://docs.python.org/library/trace.html

--ignore-module--ignore-dir命令行選項,以便最終究其原因,我想知道如何忽略所有的標準庫模塊時使用tracesys.settrace

編輯:我希望它在virtualenv內工作。 http://pypi.python.org/pypi/virtualenv

EDIT2:我希望它適用於所有環境工作(即跨操作系統,內部和外部的virtualenv的。)

+0

這是http://stackoverflow.com/questions/5632980/list-of-all-imports-in-的一個粗略的副本python-3,不幸的是到目前爲止還沒有吸引任何特別有用的答案。 –

回答

8

爲什麼不自己研究標準庫的哪些部分?

import distutils.sysconfig as sysconfig 
import os 
std_lib = sysconfig.get_python_lib(standard_lib=True) 
for top, dirs, files in os.walk(std_lib): 
    for nm in files: 
     if nm != '__init__.py' and nm[-3:] == '.py': 
      print os.path.join(top, nm)[len(std_lib)+1:-3].replace('\\','.') 

abc 
aifc 
antigravity 
--- a bunch of other files ---- 
xml.parsers.expat 
xml.sax.expatreader 
xml.sax.handler 
xml.sax.saxutils 
xml.sax.xmlreader 
xml.sax._exceptions 

編輯:你可能想增加一個檢查,以避免site-packages,如果你需要避免不規範的庫模塊。

+0

'sysconfig.get_python_lib(standard_lib = True)'也給了我沒有所有標準庫模塊的virtualenv的路徑。 – saltycrane

+0

對於virtualenv,您可以將問題簡化爲找到virtualenv的位置,該線程(http://groups.google.com/group/python-virtualenv/browse_thread/thread/e30029b2e50ae17a)建議可以使用'sys.real_prefix'完成(雖然我沒有virtualenv方便測試) – Caspar

+0

在virtualenv中使用'sys.real_prefix'也在[this SO answer]中提到(http://stackoverflow.com/questions/1871549/python -deven-if-running-inside-virtualenv/1883251#1883251) – Caspar

1

這將讓你接近:

import sys; import glob 
glob.glob(sys.prefix + "/lib/python%d.%d" % (sys.version_info[0:2]) + "/*.py") 

另一種可能性爲ignore-dir選項:

os.pathsep.join(sys.path) 
+0

我剛剛意識到'sys.prefix'返回的路徑不包含大部分標準庫模塊,當我在virtualenv中運行時。我上面編輯了我的問題。 – saltycrane

1

我會在官方文檔中查閱標準庫的參考資料,該文檔會通過整個庫以及每個模塊的章節。 :)

4

這裏有卡斯帕的答案,這是不是跨平臺的改善,並錯過了頂層模塊(例如email),動態加載模塊(如array),以及核心內建模塊(例如sys) :

import distutils.sysconfig as sysconfig 
import os 
import sys 

std_lib = sysconfig.get_python_lib(standard_lib=True) 

for top, dirs, files in os.walk(std_lib): 
    for nm in files: 
     prefix = top[len(std_lib)+1:] 
     if prefix[:13] == 'site-packages': 
      continue 
     if nm == '__init__.py': 
      print top[len(std_lib)+1:].replace(os.path.sep,'.') 
     elif nm[-3:] == '.py': 
      print os.path.join(prefix, nm)[:-3].replace(os.path.sep,'.') 
     elif nm[-3:] == '.so' and top[-11:] == 'lib-dynload': 
      print nm[0:-3] 

for builtin in sys.builtin_module_names: 
    print builtin 

這仍然是不完美的,因爲它會錯過像os.path這是從os.py內依賴於平臺的方式通過代碼,如import posixpath as path定義的東西,但它可能不如你會得到,軸承記住Python是一種動態語言,你不能真正知道哪些模塊直到它們被定義實際上是在運行時定義的。

2

這裏有一個2014的回答2011年的問題 -

isort筆者,一個工具,清理進口,必須以滿足該核心庫進口前應責令PEP8要求搏鬥此相同的問題第三方進口。

我一直在使用這個工具,它似乎工作得很好。您可以將文件isort.py中使用的方法place_module,因爲它是開源的,我希望作者不會介意我在這裏再現邏輯:

def place_module(self, moduleName): 
    """Tries to determine if a module is a python std import, third party import, or project code: 

    if it can't determine - it assumes it is project code 

    """ 
    if moduleName.startswith("."): 
     return SECTIONS.LOCALFOLDER 

    index = moduleName.find('.') 
    if index: 
     firstPart = moduleName[:index] 
    else: 
     firstPart = None 

    for forced_separate in self.config['forced_separate']: 
     if moduleName.startswith(forced_separate): 
      return forced_separate 

    if moduleName == "__future__" or (firstPart == "__future__"): 
     return SECTIONS.FUTURE 
    elif moduleName in self.config['known_standard_library'] or \ 
      (firstPart in self.config['known_standard_library']): 
     return SECTIONS.STDLIB 
    elif moduleName in self.config['known_third_party'] or (firstPart in self.config['known_third_party']): 
     return SECTIONS.THIRDPARTY 
    elif moduleName in self.config['known_first_party'] or (firstPart in self.config['known_first_party']): 
     return SECTIONS.FIRSTPARTY 

    for prefix in PYTHONPATH: 
     module_path = "/".join((prefix, moduleName.replace(".", "/"))) 
     package_path = "/".join((prefix, moduleName.split(".")[0])) 
     if (os.path.exists(module_path + ".py") or os.path.exists(module_path + ".so") or 
      (os.path.exists(package_path) and os.path.isdir(package_path))): 
      if "site-packages" in prefix or "dist-packages" in prefix: 
       return SECTIONS.THIRDPARTY 
      elif "python2" in prefix.lower() or "python3" in prefix.lower(): 
       return SECTIONS.STDLIB 
      else: 
       return SECTIONS.FIRSTPARTY 

    return SECTION_NAMES.index(self.config['default_section']) 

很明顯,你需要在類和的情況下使用這種方法設置文件。這基本上是一個已知核心庫導入的靜態列表的回退。

# Note that none of these lists must be complete as they are simply fallbacks for when included auto-detection fails. 
default = {'force_to_top': [], 
      'skip': ['__init__.py', ], 
      'line_length': 80, 
      'known_standard_library': ["abc", "anydbm", "argparse", "array", "asynchat", "asyncore", "atexit", "base64", 
             "BaseHTTPServer", "bisect", "bz2", "calendar", "cgitb", "cmd", "codecs", 
             "collections", "commands", "compileall", "ConfigParser", "contextlib", "Cookie", 
             "copy", "cPickle", "cProfile", "cStringIO", "csv", "datetime", "dbhash", "dbm", 
             "decimal", "difflib", "dircache", "dis", "doctest", "dumbdbm", "EasyDialogs", 
             "errno", "exceptions", "filecmp", "fileinput", "fnmatch", "fractions", 
             "functools", "gc", "gdbm", "getopt", "getpass", "gettext", "glob", "grp", "gzip", 
             "hashlib", "heapq", "hmac", "imaplib", "imp", "inspect", "itertools", "json", 
             "linecache", "locale", "logging", "mailbox", "math", "mhlib", "mmap", 
             "multiprocessing", "operator", "optparse", "os", "pdb", "pickle", "pipes", 
             "pkgutil", "platform", "plistlib", "pprint", "profile", "pstats", "pwd", "pyclbr", 
             "pydoc", "Queue", "random", "re", "readline", "resource", "rlcompleter", 
             "robotparser", "sched", "select", "shelve", "shlex", "shutil", "signal", 
             "SimpleXMLRPCServer", "site", "sitecustomize", "smtpd", "smtplib", "socket", 
             "SocketServer", "sqlite3", "string", "StringIO", "struct", "subprocess", "sys", 
             "sysconfig", "tabnanny", "tarfile", "tempfile", "textwrap", "threading", "time", 
             "timeit", "trace", "traceback", "unittest", "urllib", "urllib2", "urlparse", 
             "usercustomize", "uuid", "warnings", "weakref", "webbrowser", "whichdb", "xml", 
             "xmlrpclib", "zipfile", "zipimport", "zlib", 'builtins', '__builtin__'], 
      'known_third_party': ['google.appengine.api'], 
      'known_first_party': [], 

---剪斷---

我已經是一個小時到之前,我迷迷糊糊的isort模塊自己寫這個工具,所以我希望這也能幫助別人,以避免重新發明車輪!

21

如果有人在2015年仍在閱讀本文,我遇到了同樣的問題,並且不喜歡任何現有的解決方案。因此,我通過編寫一些代碼在官方Python文檔中刪除標準庫頁面的TOC來強制它。我還構建了一個用於獲取標準庫列表的簡單API(適用於Python版本2.6,2.7,3.2,3.3和3.4)。

該軟件包是here,它的用法很簡單:

>>> from stdlib_list import stdlib_list 
>>> libraries = stdlib_list("2.7") 
>>> libraries[:10] 
['AL', 'BaseHTTPServer', 'Bastion', 'CGIHTTPServer', 'ColorPicker', 'ConfigParser', 'Cookie', 'DEVICE', 'DocXMLRPCServer', 'EasyDialogs'] 
+1

最佳解決方案!非常感謝你!!! – ragesz

相關問題