查找导入的用户定义模块

时间:2014-02-28 12:34:05

标签: python module

我找到了模块modulefinder

  

可用于确定脚本导入的模块集。

但是,当我对我的脚本执行示例测试时,它只检查基本模块,并且不报告它导入的非基础模块(“ - ”是我添加到正确的可读格式):

Loaded modules:  

- tokenize:  LPAR,PseudoToken,ENDMARKER  
- heapq:  sort,izip,_nlargest  
- __future__:  __module__,getMandatoryRelease,division  
- copy_reg:  _inverted_registry,_slotnames,__all__  
- sre_compile:  _optimize_unicode,isstring,_sre  
- _collections:    
- cStringIO:    
- _sre:    
- functools:  wraps,partial,WRAPPER_ASSIGNMENTS  
- random:  WichmannHill,_sin,_exp  
- cPickle:  
- __builtin__:  
- subprocess:  __module__,STDOUT,__str__  
- dummy_thread:  __module__,__enter__,__exit__  
- gc:  
- cmd:  do_help,__module__,prompt  
- __main__:  PREDICTORS_CFG_DIR,sequence,tearDown  
- operator:   
- array:  
- select:  
- _heapq:  
- _threading_local:  current_thread,__module__,__new__  
- abc:  __module__,_InstanceType,_abc_invalidation_counter  
- _bisect:  
- posixpath:  _varprog,sameopenfile,splitext  
- _random:    
- os2emxpath:  pardir,realpath,stat  
- tempfile:  _TemporaryFileWrapper,_set_cloexec,_candidate_tempdir_list  
- errno:  
- pprint:  __module__,format,_StringIO  
- binascii:   
- token:  DEDENT,LPAR,ENDMARKER  
- sre_constants:  REPEAT_ONE,makedict,AT_END_LINE  
- re:  __module__,finditer,_expand  
- _abcoll:  __module__,Container,__isub__  
- collections:  __repr__,OrderedDict,_make  
- ntpath:  pardir,realpath,exists  
- threading:  _BoundedSemaphore,_sleep,Semaphore  
- opcode:  hasjrel,hasconst,EXTENDED_ARG  
- _struct:    
- _warnings:  
- math:  
- shlex:  __module__,get_token,file  
- fcntl:  
- genericpath:  isdir,stat,commonprefix  
- stat:  S_IWRITE,ST_MTIME,S_IRGRP  
- string:  ascii_lowercase,upper,__module__  
- warnings:  __module__,filterwarnings,once_registry  
- UserDict:  __module__,popitem,pop  
- inspect:  CO_VARARGS,formatargvalues,findsource  
- repr:  aRepr,__module__,repr_list  
- struct:  _clearcache,__doc__  
- sys:  
- pwd:  
- imp:  
- getopt:  opt,__module__,short_has_arg  
- readline:  
- copy:  _copy_with_copy_method,__module__,_deepcopy_atomic  
- ConfigParser:  optionxform,InterpolationError,set  
- bdb:  __module__,get_file_breaks,clear_bpbynumber  
- types:  __module__,IntType,TypeType  
- strop:  
- _functools:  
- keyword:  iskeyword,main,kwlist  
- thread:  
- StringIO:  __module__,readlines,getvalue  
- bisect:  insort_right,bisect,bisect_left  
- pickle:  EMPTY_DICT,NEWTRUE,TypeType  
- signal:  
- traceback:  print_stack,print_exception,print_last  
- difflib:  _count_leading,heapq,_namedtuple  
- marshal:  
- linecache:  updatecache,clearcache,__all__  
- itertools:  
- posix:  
- doctest:  REPORT_UDIFF,get_doctest,_parse_example
- unittest:  parseArgs,assertNotEquals,__str__  
- time:  
- sre_parse:  _PATTERNENDERS,SRE_FLAG_UNICODE,AT_END  
- os:  pardir,__module__,sep  
- pdb:  do_enable,help,help_run  
- dis:  hasjrel,hasconst,HAVE_ARGUMENT

当我的模块只有这个输入时:

import os
import unittest #User-Defined
from ConfigParser import ConfigParser #User-Defined


from common import RNASPACE_DIR, TEST_DIR_NAME, DATA_DIR_NAME, \
                   TEST_USER_DIR_NAME, WORKSPACE_DIR, SEQ_FASTA_SUFFIX, \
                   TEST_PROJECT_NAME, SEQ_DIR_NAME, SEQ_TXT_SUFFIX, \
                   PREDICTORS_CFG_DIR, PREDICTOR_CFG_SUFFIX, OUTPUT_DIR_NAME, \
                   TEST_RUN_NAME, OUTPUT_FILE_SUFFIX, REFERENCE_DIR_NAME, \
                   REFERENCE_FILE_SUFFIX
#User-Defined

import sys 
path_rnaspace = "/bi/opt/RNAspace/rnaspace_sources/rnaspace/rnaspace/"
sys.path.append(path_rnaspace) 

from rnaspace.core.sequence import sequence #User-Defined
from rnaspace.core.conversion.gff_converter import gff_converter #User-Defined
from rnaspace.core.conversion.csv_converter import csv_converter #User-Defined
from rnaspace.core.conversion.fasta_converter import fasta_converter #User-Defined

Here我找到了一些解决方案但是我不能应用它们,或者它们有一些问题或者不起作用。

如何获取导入的用户定义模块?

1 个答案:

答案 0 :(得分:0)

如果您仔细阅读该示例,您将看到modulefinder.ModuleFinder报告导入的模块而不仅仅是存在导入语句的模块。这是因为一个模块可以导入其他模块,而这些模块又可以导入其他模块,依此类推。

我怀疑你的程序,主要是导入unittest(这是一个复杂的模块),确实导入了所有这些模块。如果您不相信,请修改程序以打印出sys.modules的密钥,这是查找已导入模块的简便方法。