相关疑难解决方法(0)

返回脚本中使用的导入Python模块列表?

我正在编写一个程序,用于对导入模块的Python文件列表进行分类.因此,我需要扫描.py文件的集合,并返回它们导入的模块的列表.例如,如果我导入的其中一个文件包含以下行:

import os
import sys, gtk
Run Code Online (Sandbox Code Playgroud)

我希望它返回:

["os", "sys", "gtk"]
Run Code Online (Sandbox Code Playgroud)

我玩modulefinder并写道:

from modulefinder import ModuleFinder

finder = ModuleFinder()
finder.run_script('testscript.py')

print 'Loaded modules:'
for name, mod in finder.modules.iteritems():
    print '%s ' % name,
Run Code Online (Sandbox Code Playgroud)

但这不仅仅返回脚本中使用的模块.作为脚本中的一个例子,它只包含:

import os
print os.getenv('USERNAME')
Run Code Online (Sandbox Code Playgroud)

从ModuleFinder脚本返回的模块返回:

tokenize  heapq  __future__  copy_reg  sre_compile  _collections  cStringIO  _sre  functools  random  cPickle  __builtin__  subprocess  cmd  gc  __main__  operator  array  select  _heapq  _threading_local  abc  _bisect  posixpath  _random  os2emxpath  tempfile  errno  pprint  binascii  token  sre_constants  re  _abcoll  collections  ntpath  threading  opcode  _struct  _warnings  math  shlex …
Run Code Online (Sandbox Code Playgroud)

python module

28
推荐指数
4
解决办法
3万
查看次数

标签 统计

module ×1

python ×1