python sys模块sys.path用法示例

发布时间:2020-01-29编辑:脚本学堂
本文介绍了python sys模块sys.path的用法,python sys模块包含了与python解释器和它的环境有关的函数,感兴趣的朋友参考下。

python sys模块包含了与python解释器和它的环境有关的函数,可以通过dir(sys)来查看其方法与成员属性

例1,python sys模块用法
 

复制代码 代码示例:
import sys
print dir(sys)
result:
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']

例2:
 

复制代码 代码示例:
import sys
print sys.path
result:
['C:Documents and SettingsusernameMy DocumentsAptana Studio 3 WorkspacePython_Test_Projectsrc', 'C:Documents and SettingsusernameMy DocumentsAptana Studio 3 WorkspacePython_Test_Projectsrc', 'C:Python27', 'C:Python27DLLs', 'C:Python27lib', 'C:Python27liblib-tk', 'C:Python27libplat-win', 'C:Python27libsite-packages', 'C:Python27libsite-packageswx-2.8-msw-unicode', 'C:WINDOWSsystem32python27.zip']
 

其中有个 sys.path属性。

它是一个list.默然情况下python导入文件或模块,会先在sys.path里找模块的路径。若没有,则程序就会报错。

最好把模块路径给加到当前模块扫描的路径中,例如:sys.path.append('你的模块的名称'),如此程序就不会因找不到模块而报错。